examples icon indicating copy to clipboard operation
examples copied to clipboard

Add an Go stackref example

Open jaxxstorm opened this issue 5 years ago • 0 comments

@EvanBoyle wrote this in slack, and I think it'd be useful to put here somewhere:

package main
import (
	"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		ref, err := pulumi.NewStackReference(ctx, "EvanBoyle/ref1/dev", nil)
		if err != nil {
			return err
		}
		strarr := ref.GetOutput(pulumi.String("ref")).ApplyStringArray(func(out interface{}) []string {
			var res []string
			if out != nil {
				for _, v := range out.([]interface{}) {
					res = append(res, v.(string))
				}
			}
			return res
		})
		idx0 := strarr.ApplyString(func(a []string) string {
			var res string
			if len(a) > 0 {
				res = a[0]
			}
			return res
		})
		ctx.Export("idx0", idx0)
		return nil
	})
}

jaxxstorm avatar Jul 20 '20 19:07 jaxxstorm