Using Go Generics with Pulumi
Pulumi loves Go, it’s what powers Pulumi. We’ve kept a close eye on the design and development of support for generics in the Go programming language over the years, a feature that allows developers to write type-safe, concise, and reusable code. We’ve been exploring what it’d look like to improve Pulumi’s Go SDKs with generics and recently published a public RFC detailing our plans. We’ve been making progress on the implementation and are excited to announce preview support for Go generics in our core and AWS Go SDKs. If you’re using Go with Pulumi, we’d love for you to give it a try and share your feedback!
// Given
var a pulumi.IntOutput
var b pulumi.StringOutput
// Before (could panic at runtime if you got something wrong)
o := pulumi.All(a, b).ApplyT(func(vs []interface{}) string { // could panic
a := vs[0].(int) // could panic
b := vs[1].(string) // could panic
return strconv.Itoa(a) + b
}).(pulumi.StringOutput) // could panic
// After (compile-time type-safety)
o := pulumix.Apply2(a, b, func(a int, b string) string {
return strconv.Itoa(a) + b
})