golang 函数返回局部变量地址

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)
}

上一篇:查看运算顺序,执行顺序


下一篇:Go 指针相关操作