在AWS CDK中使用Go覆盖AvailabilityZones(接口中的函数)

huangapple go评论102阅读模式
英文:

Override AvailabilityZones (Function in Interface) in AWS CDK With Go

问题

Stack.AvailabilityZones的文档中写道:

// 要指定选择可用区的不同策略,请重写此方法。
AvailabilityZones() *[]*string

这是我尝试实现的代码:

stack := awscdk.NewStack(scope, &id, &sprops)
stack.AvailabilityZones = func() *[]*string {      // 这是第22行
	return &[]*string{jsii.String("us-west-1a")}
}

然而,我得到了错误:

[andrew@localhost cdk]$ go run vpc.go common.go 
./vpc.go:22:2: cannot assign to stack.AvailabilityZones (value of type func() *[]*string)

我应该如何做才对?

我不确定我是否犯了一个愚蠢的错误,或者我是否需要完全不同的做法(在Go中,“override”是什么意思,它不是面向对象的?编辑:或者至少不是以一种我可以从Python示例中复制的方式)。

英文:

The documentation for Stack.AvailabilityZones says:

// To specify a different strategy for selecting availability zones override this method.
AvailabilityZones() *[]*string

This is my code that attempts to do this:

stack := awscdk.NewStack(scope, &id, &sprops)
stack.AvailabilityZones = func() *[]*string {      // this is line 22
	return &[]*string{jsii.String("us-west-1a")}
}

However, I get the error:

[andrew@localhost cdk]$ go run vpc.go common.go 
./vpc.go:22:2: cannot assign to stack.AvailabilityZones (value of type func() *[]*string)

How should I be doing this?

I am not sure if I have made a stupid mistake or if I need to do something completely different (what does "override" mean in go which is not OO? edit: or at least not in a way that means I can copy from a python example).

答案1

得分: 1

Go有一种继承和“覆盖”方法的方式。你可以通过在结构体中“嵌入”另一个类型(不给类型命名)来实现继承;参考:https://stackoverflow.com/questions/21527489/go-inheritance-using-anonymous-type-in-a-struct-as-a-method-parameter

你的结构体将会继承嵌入类型的所有方法。你可以通过在结构体上直接声明方法来覆盖其中的某些方法。

一个简化的示例如下:

type MyStack struct {
	awscdk.Stack
}

func (s *MyStack) AvailabilityZones() *[]string {
	return &[]string{"foo", "bar"}
}

func f() {
	// ...
	stack := awscdk.NewStack(scope, &id, &sprops)
	myStack := &MyStack{Stack: stack}
	fmt.Println(myStack.AvailabilityZones())
}

编辑:以下是我(Andrew Cooke)实际测试通过的代码(但没有解决我的更大问题,所以没有进行深度测试):

type ZoneStack struct {
	awscdk.Stack
}

func (stack *ZoneStack) AvailabilityZones() *[]*string {
	return &[]*string{jsii.String("us-west-1a")}
}

...

zstack := ZoneStack{}
awscdk.NewStack_Override(&zstack, scope, &id, &sprops)

上面的原始答案可以编译但会出现与JS序列化相关的运行时错误!)
英文:

Go has a way to inherit and "override" methods. You inherit by "embedding" another type inside a struct (where you don't name the type); see: https://stackoverflow.com/questions/21527489/go-inheritance-using-anonymous-type-in-a-struct-as-a-method-parameter

Your struct will then inherit all the methods from the embedded type.
And you can override individual ones by declaring them directly on the struct.

With a simplified example, that would look like:

type MyStack struct {
	awscdk.Stack
}

func (s *MyStack) AvailabilityZones() *[]string {
	return &[]string{"foo", "bar"}
}

func f() {
    // ...
	stack := awscdk.NewStack(scope, &id, &sprops)
	myStack := &MyStack{Stack: stack}
	fmt.Println(myStack.AvailabilityZones())
}

edit: this is what actually worked for me (andrew cooke) (but didn't solve my larger problem, so isn't deeply tested)

type ZoneStack struct {
    awscdk.Stack
}

func (stack *ZoneStack) AvailabilityZones() *[]*string {
    return &[]*string{jsii.String("us-west-1a")}
}

...

zstack := ZoneStack{}
awscdk.NewStack_Override(&zstack, scope, &id, &sprops)

(the original answer above compiles, but gives runtime errors that seem to be related to JS serialization?!)

huangapple
  • 本文由 发表于 2022年9月1日 22:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/73570664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定