Skip to content

Commit ff920de

Browse files
docs: add documentation for the new type(slice of any) in Go (#474)
## Description **Documentation Change Type:** - [ ] Typo/grammar fix - [ ] Clarification or minor improvement - [x] New example or code snippet - [x] New section or topic - [ ] Major restructuring or enhancement **Affected Documentation Page(s):** - https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-go.html ## Description of Changes We're changing the props type in some constructs, and for this we created new helper functions introduced in [this PR](aws/jsii#4936) ## Motivation and Context <!-- Why are these changes required? What problem do they solve? --> ## How Has This Been Validated? Using Github Preview ## Screenshots (if appropriate) <img width="1657" height="1321" alt="Screenshot 2025-09-30 at 15 29 10" src="https://github.com/user-attachments/assets/6708bbc3-f0f9-4e08-a398-727338804577" /> <img width="1657" height="1321" alt="Screenshot 2025-09-30 at 15 29 17" src="https://github.com/user-attachments/assets/0d5eed7a-b22c-40fd-8001-f41cc6822fc1" /> ## Checklist - [x] My changes follow the [AsciiDoc syntax](./CONTRIBUTING.md#asciidoc-syntax-reference) guidelines - [x] I have previewed my changes using GitHub's preview or a local AsciiDoc renderer - [x] My changes maintain consistent formatting with the rest of the documentation - [x] I have checked that all links work correctly ## Additional Information <!-- Any additional information or context that might be helpful for reviewers --> --------- Co-authored-by: Kristina Peterson <kripet@amazon.com>
1 parent cebf6cd commit ff920de

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

v2/guide/work-with-cdk-go.adoc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,41 @@ The CDK provides variadic helper functions such as `jsii.Strings` for building s
144144
jsii.Strings("One", "Two", "Three")
145145
----
146146
147+
[#go-any-slice]
148+
=== Working with any slice
149+
150+
Certain constructs expect properties that are a list of multiple types (union types in TypeScript). In [.noloc]`Go`, these are as a slice of any (`*[]any`). The `any` ensures that the compiler will allow the assignment of different types there. Refer to the documentation for the `https://pkg.go.dev/github.com/aws/aws-cdk-go/awscdk/v2[{aws} CDK Go package]` to find out what the allowed types are.
151+
152+
To work with such properties, use the helper functions provided by `jsii` to create slices of any from different types:
153+
154+
* `jsii.AnySlice`
155+
* `jsii.AnyStrings`
156+
* `jsii.AnyNumbers`
157+
158+
For example:
159+
160+
[source,go,subs="verbatim,attributes"]
161+
----
162+
func Arns() *[]*string {
163+
a := "arn:aws:s3:::bucket1"
164+
b := "arn:aws:s3:::bucket2"
165+
return &[]*string{&a, &b}
166+
}
167+
168+
awsiam.NewCfnUser(stack, jsii.String("User"), &awsiam.CfnUserProps{
169+
ManagedPolicyArns: jsii.AnySlice(Arns())
170+
// or
171+
ManagedPolicyArns: jsii.AnyStrings("arn:aws:s3:::bucket1", "arn:aws:s3:::bucket2")
172+
// or
173+
ManagedPolicyArns: &[]interface{}{
174+
jsii.String("arn:aws:s3:::bucket1"),
175+
jsii.String("arn:aws:s3:::bucket2"),
176+
}
177+
})
178+
----
179+
180+
This approach ensures that your slices are correctly interpreted by the CDK, avoiding deserialization errors when deploying or synthesizing your stacks.
181+
147182
[#go-writing-constructs]
148183
=== Developing custom constructs
149184
@@ -156,4 +191,15 @@ If you are simply changing some default values on an existing construct or addin
156191
157192
The {aws} CDK automatically compiles your app before running it. However, it can be useful to build your app manually to check for errors and to run tests. You can do this by issuing `go build` at a command prompt while in your project's root directory.
158193
159-
Run any tests you've written by running `go test` at a command prompt.
194+
Run any tests you've written by running `go test` at a command prompt.
195+
196+
[#go-troubleshooting]
197+
== Troubleshooting
198+
199+
If you encounter a compiler error like the following, it means that string slices were passed directly to a property expecting a slice of any.
200+
201+
[source,none]
202+
----
203+
Cannot use 'jsii.Strings("arn:aws:s3:::bucket1", "arn:aws:s3:::bucket2")' (type *[]*string) as the type *[]interface{}
204+
----
205+
To resolve this error, replace `jsii.Strings()` with `jsii.AnyStrings()`. See this `https://github.com/aws/aws-cdk/issues/35630[CDK GitHub issue]` for more context and additional solutions.

0 commit comments

Comments
 (0)