Go pointer basis of language

Different from the C / C ++ in the pointer, the pointer Go language can not be shifted and operation are safe pointer.

Go wants to understand the language of pointers you need to know three concepts: an address pointer, pointer types, and pointer values.

Go language pointer

Go language function parameters are passed to copy values, when we want to modify a variable, we can create a pointer variable that points to the address of a variable. Transfer data pointer, without copying the data. Type and pointer offset calculation can not be performed. Go language pointer operation is very simple, only need to remember two symbols: &(address), and *(the address value).

And pointer type pointer address

Each variable at run time has an address, the address represents the variable location in memory. Using the Go language &character variables placed in front of the variable "fetch address" operation. Value type (int, float, bool, string , array, struct) Go language has a corresponding pointer type, such as: *int, *int64, *stringand the like.

Take the pointer variable syntax is as follows:

ptr := &v    // v的类型为T

among them:

  • v: is taken to be representative of the address of the variable, the type ofT
  • ptr: receiving a variable address, it is PTR type *T, pointer type T is called. * Represents the pointer.

for example:

func main() { a := 10 b := &a fmt.Printf("a:%d ptr:%p\n", a, &a) // a:10 ptr:0xc00001a078 fmt.Printf("b:%p type:%T\n", b, b) // b:0xc00001a078 type:*int fmt.Println(&b) // 0xc00000e018 } 

Let's look at b := &aan illustration:Take variable address shown

Pointer Value

After using the & operator takes the address pointer will get one of ordinary variable this variable can then be used for the operation * pointer, i.e. the pointer value, as follows.

func main() { //指针取值 a := 10 b := &a // 取变量a的地址,将指针保存到b中 fmt.Printf("type of b:%T\n", b) c := *b // 指针取值(根据指针去内存取值) fmt.Printf("type of c:%T\n", c) fmt.Printf("value of c:%v\n", c) } 

Output is as follows:

type of b:*int
type of c:int value of c:10 

Summary: address-operator &and operators values *is a pair of complementary operator, &fetch address, *the address pointed to by the address value extracted.

Variables, characteristics and the relationship between the address pointers, pointer variable, taking the address, the value is as follows:

  • Taken to address variable (&) operation can be obtained pointer variable this variable.
  • Value of the pointer variable is a pointer address.
  • Pointer variable value (*) value of the original variable operation can be obtained pointer variable points.

Example value pointer pass:

func modify1(x int) { x = 100 } func modify2(x *int) { *x = 100 } func main() { a := 10 modify1(a) fmt.Println(a) // 10 modify2(&a) fmt.Println(a) // 100 } 

and make new

Let's look at an example:

func main() { var a *int *a = 100 fmt.Println(*a) var b map[string]int b["沙河娜扎"] = 100 fmt.Println(b) } 

The implementation of the code above will lead to panic, why? Go to language in reference type variable, we must not only declare it when in use, but also to allocate memory space for it, otherwise we have no way to store value. As for the value of the type of statement you do not need to allocate memory space, because they have a good default memory space allocated at the time of declaration. To allocate memory, it leads out of today's new and make. Go and make new languages ​​are two built-in functions, mainly used to allocate memory.

new

is a new built-in functions, its function signature as follows:

func new(Type) *Type 

among them,

  • Type indicates the type, new function accepts a parameter, which is a type
  • * Type indicates the type of pointer, new function returns a pointer to the memory address type.

less common new function, using a new function obtained is the type of pointer, and the value of the zero value corresponding to the type of the pointer. for example:

func main() { a := new(int) b := new(bool) fmt.Printf("%T\n", a) // *int fmt.Printf("%T\n", b) // *bool fmt.Println(*a) // 0 fmt.Println(*b) // false } 

The sample code in the beginning of this section var a *intonly declares a pointer variable but did not initialize a pointer as the reference type and have a need to initialize memory space after, you can assign to it. Should use the new built-in functions can be normal after their assignment for a initialized as follows:

func main() { var a *int a = new(int) *a = 10 fmt.Println(*a) } 

make

also make for memory allocation, different from new, only used slice, map and memory chan created, and type it returns is three type itself, rather than their pointer type, because these three types is reference types, so there is no need to return to their hands up. Function signature make the following functions:

func make(t Type, size ...IntegerType) Type 

make function is irreplaceable, we use the slice, map and channel, we both need to make to initialize before you can operate on them. This we have explained in the previous chapter, we will explain in detail about the channel in subsequent chapters.

The examples in this section beginning in var b map[string]intjust declare a variable b is a variable of type map, and then need something like the following sample code as make use initialization function can be assigned key-value pairs:

func main() { var b map[string]int b = make(map[string]int, 10) b["沙河娜扎"] = 100 fmt.Println(b) } 

New and make a difference

  1. Both are used for memory allocation.
  2. make only for initializing slice, map and channel, or the return of the three types of reference itself;
  3. And for the new type of memory allocation, memory type and a value corresponding to the zero value of the return pointer is a pointer type.

Guess you like

Origin www.cnblogs.com/zhaohaiyu/p/11348872.html