综合知识Go单选难度 3真题改编 · 2024
112. 下面 Go 程序的运行结果是()
package main
import "fmt"
type CustomStruct struct {
A int
}
type Options func(*CustomStruct)
func WithAddValue(value int) Options {
return func(c *CustomStruct) {
c.A += value
}
}
func WithMinusValue(value int) Options {
return func(c *CustomStruct) {
c.A -= value
}
}
func NewCustomStruct(initValue int, opts ...Options) *CustomStruct {
res := &CustomStruct{
A: initValue,
}
for _, opt := range opts {
opt(res)
}
return res
}
func main() {
stru := NewCustomStruct(5, WithAddValue(-1), WithAddValue(2), WithMinusValue(2))
fmt.Println(stru.A)
}
练习本题 ›