golang 函数返回局部变量地址

墨春

关注

阅读 149

2022-01-07

https://groups.google.com/g/golang-china/c/xz9JdKqHi-c

func fn() *int {
a := 10
return &a
}

type Config struct {
val int
str string
}

func fn2() *Config {
b := &Config{val: 5,
str: "ni"}
return b
}

func main() {

x2 := fn2()
x3 := fn2()
fmt.Println("x address:", x2)
fmt.Println("x=", *x2)
fmt.Println("x address:", x3)
fmt.Println("x=", *x3)

x := fn()
y := fn()
fmt.Println("x address:", x)
fmt.Println("x=", *x)
fmt.Println("y address:", y)
fmt.Println("y=", *y)
}

精彩评论(0)

0 0 举报