当前位置:网站首页>Golang ---- storage of variables and their contents

Golang ---- storage of variables and their contents

2022-06-09 04:08:00 It artist rookie

First ,& It's address symbol , You can get the memory address of the variable

var a int
	a = 45
	fmt.Println("&a=", &a) //=>&a= 0xc00000c2b8 16 Base number 

This address is different from Java Address of variable in . The address taken here , We can say that it is the address of the variable , You could say 45 The address where this value is located , in other words , The address of a variable is the same as the address of its value ( Arrays are not included here , section ,map These complex types of variables ).
Java The memory address where the value is stored in the variable in , That is to say , The address of a variable and the address of its value are not the same

The address of the variable of the slice type

That is, the address of the first element of the slice

var arr []float32
	fmt.Printf("%p&arr=\n", arr)
	arr = append(arr, []float32{
    11.0, 22, 12, 24}...)
	fmt.Printf("%parr=\n", arr)

	fmt.Println("&arr=", &arr[0])
	fmt.Println("&arr=", &arr[1])
	fmt.Println("&arr=", &arr[2])
	fmt.Println("&arr=", &arr[3])
	fmt.Printf("%parr=\n", arr)
//0x0&arr=
//0xc00000c2d0arr=  Same as the address of the first element of the slice 
//&arr= 0xc00000c2d0
//&arr= 0xc00000c2d4
//&arr= 0xc00000c2d8
//&arr= 0xc00000c2dc
//0xc00000c2d0arr=

原网站

版权声明
本文为[It artist rookie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090407034995.html