The generated go file

See the whole file from the source link below. We will have a look at the different parts. Click on the tabs to see the code and the explanation.

  3 import (
  4         "github.com/aws/aws-cdk-go/awscdk/v2"
  5         "github.com/aws/aws-cdk-go/awscdk/v2/awssns"
  6         "github.com/aws/constructs-go/constructs/v10"
  7         "github.com/aws/jsii-runtime-go"
  8 )
 10 type HellocdkStackProps struct {
 11         awscdk.StackProps
 12 }
 31 func main() {
 32         app := awscdk.NewApp(nil)
 33
 34         NewHellocdkStack(app, "HellocdkStack", &HellocdkStackProps{
 35                 awscdk.StackProps{
 36                         Env: env(),
 37                 },
 38         })
 39
 40         app.Synth(nil)
 41 }
 14 func NewHellocdkStack(scope constructs.Construct, id string, props *HellocdkStackProps) awscdk    .Stack {
 15         var sprops awscdk.StackProps
 16         if props != nil {
 17                 sprops = props.StackProps
 18         }
 19         stack := awscdk.NewStack(scope, &id, &sprops)
 20
 21         // The code that defines your stack goes here
 22
 23         // as an example, here's how you would define an AWS SNS topic:
 24         awssns.NewTopic(stack, jsii.String("MyTopic"), &awssns.TopicProps{
 25                 DisplayName: jsii.String("MyCoolTopic"),
 26         })
 27
 28         return stack
 29 }

Necessary imports.

Line purpose
4 The CDK itself. This is needed in any case
5 The module for the infrastructure. You need the modules for the infrastructure types you want to define. Here SNS
6  The construct library.This is needed in any case.
7  You may replace this with the aws sdk module

Properties to use the construct

This is the input property for the construct. Here only the awscdk.StackProps are defined, so you would not need different props for each stack type. But defining a new type of props for each constructs is more flexible. Notice that the names here also are derived from the directory name you started with.

The “manager” part, the main program

In line 32 the CDK App is created with a pointer to AppProps. Because this is a pointer variable, nil is a valid (empty) entry.

In line 34 the construct New function is called. This is just a function, not an object constructor as in other CDK languages, just because GO has a simpler approach to object orientation, so there are no constructs.

Also in line 34 &HellocdkStackProps{…} means a pointer (&) to the HellocdkStackProps structure that follows. You could also define that outside of the function NewHellocdkStack in a variable and reference this variable,

The only property from HellocdkStackProps is awscdk.StackProps. This is a short form for:

	NewHellocdkStack(app, "HellocdkStack", &HellocdkStackProps{
		StackProps: awscdk.StackProps{
			Env: env(),
		},
	})

Line 11 do not define the props as a pointer:

 11         awscdk.StackProps

So the property is

awscdk.StackProps{
			Env: env(),
		},

and not

&awscdk.StackProps{
			Env: env(),
		},

The construct itself, here you define the infrastructure

Line  purpose
15-19 create the stack itself and forward props if defined (not nil)
24-26 create a SNS topic

The jsii.String() functions are just called to create a string pointer

So this program would create the same SNS topic as if you type the following code with aws CLI:

aws sns create-topic --name MyCoolTopic

See also