Go language values, pointers, reference type

Original: https://www.jianshu.com/p/af42cb368cef

----------------------------------------------------

Pointer with C or C ++ pointers Go language is similar, but does not support the language Go pointer pointer arithmetic, thus eliminating some of the potential problems in C or C ++ program. Since the Go language has its own garbage collector, and automatically manages memory, so you do not need the Go language like C or C ++ function as using free or delete operator.

References can be used like Java and Python object pointer after the Go language of creation.

Will be a copy of the value passed to the function or method, and a method using this function or duplicate values: However, in Go, or for a Boolean variable or numeric type or string type arrays are passed by value , it will not have any effect on the original value. In general, for the Boolean variables or numeric type or string type passed by value it is very inexpensive, Go language compiler to optimize safety in the transfer process.

However, in Go, strings are immutable, and therefore during the modified string (for example, + = operator), in Go must create a new string, and then copy the original string and added to the new after a string, for large string, the cost of operation may be relatively large.

For large strings is, a value passed to the array as well. In order to address the enormous cost that may arise, Go language instead of using an array of slice array. Transmitting a transfer on the line with the string sections of the same, regardless of the length of the slice or how much capacity. The sections were modified copy operation will not need to create a new slice as like string because the slice is variable, is a reference type.

func main() {
   a := 3 b := 4 c := "abc" d := [3]int{1,2,3} fmt.Printf("main方法:a的值为 %v,b的值为 %v,c的值为 %v,d的值为 %v \n",a,b,c,d) demo(a,b,c,d) fmt.Printf("main方法:a的值为 %v,b的值为 %v,c的值为 %v,d的值为 %v \n",a,b,c,d) } func demo(a,b int,c string,d [3]int) { a = 5 b = 6 c = "efg" d[0] = 0 fmt.Printf("demo函数:a的值为 %v,b的值为 %v,c的值为 %v,d的值为 %v\n",a,b,c,d) } ----output---- main方法: a的值为 3,b的值为 4,c的值为 abc,d的值为 [1 2 3] demo函数: a的值为 5,b的值为 6,c的值为 efg,d的值为 [0 2 3] main方法: a的值为 3,b的值为 4,c的值为 abc,d的值为 [1 2 3] 

Go language reference types are: mapping (map), an array slice (slice), the channel (channel), and methods function.

Due to the presence of Go garbage collector, so when a local variable is no longer used (no longer referred to or not to act on the range) will be garbage collected off, then the local variable life cycle by their scope decision. What if we want to manage the life cycle of local variables it? Then you need to use a pointer to manage local variables, as long as there is at least a pointer to the variable, then the life cycle of the variable can be independent of the scope.

Using the pointer variables allow us to control the life cycle, not affected by the scope of the other variable costs are minimized during delivery, and the contents can easily change the variable, rather than the value of the copy operation. A pointer is a variable that actually holds the memory address of another variable, any variable is a pointer to save the memory address can be modified by the content pointer. Passing pointers are very cheap.

Before using pointer, we need to understand the meaning of two operators
① operator &: as when binary operator, a bitwise AND operation; when used as a unary operator, the return address of the variable memory.
② operator *: When used as a binary operator is multiplication operation; as when unary operators (dereference operator), the return value of the pointer variable, in fact, the release pointer reference variable, returns the value of the variable.

Creating and using the pointer, the code can be seen in the following examples

func main() {
   a := 3 p := &a //这里是获取变量a的内存地址,并将其赋值给变量p fmt.Printf("a的值为 %v, a的指针是 %v ,p指向的变量的值为 %v\n",a,p,*p) } -----output----- a的值为 3, a的指针是 0xc042060080 ,p指向的变量的值为 3 

In fact, * p and the value of variable a is equal, the two can be used to exchange, both of which are associated with the same memory address, one of the variables to modify operation affects the value of another variable, but if the variable p is pointer assignment of other variables to die.

About the integrated use of pointers, we look at the following code examples

func main() {
   a := 3 b := 4 p1 := &a //获取变量a的内存地址,并将其赋值给变量p1 p2 := &b //获取变量b的内存地址,并将其赋值给变量p2 fmt.Printf("a的值为 %v, a的指针是 %v ,p1指向的变量的值为 %v\n",a,p1,*p1) fmt.Printf("b的值为 %v, b的指针是 %v ,p2指向的变量的值为 %v\n",b,p2,*p2) fmt.Println(demo(p1,p2)) fmt.Printf("a的值为 %v, a的指针是 %v ,p1指向的变量的值为 %v\n",a,p1,*p1) fmt.Printf("b的值为 %v, b的指针是 %v ,p2指向的变量的值为 %v\n",b,p2,*p2) } func demo(a,b *int)int { *a = 5 *b = 6 return *a * *b //这里出现连续的两个*,Go编译器会根据上下文自动识别乘法与两个引用 } -----output----- a的值为 3, a的指针是 0xc042060080 ,p1指向的变量的值为 3 b的值为 4, b的指针是 0xc042060088 ,p2指向的变量的值为 4 30 a的值为 5, a的指针是 0xc042060080 ,p1指向的变量的值为 5 b的值为 6, b的指针是 0xc042060088 ,p2指向的变量的值为 6 

After the code according to the above, we can see the pointer, the value of an integer type originally passed changes to the original variables will affect the value of the function or method.

Null pointer
when a pointer is not assigned to any of the variable defined, its value is nil.
nil pointer is also called a null pointer.
null nil and other languages, None, nil, NULL as in concept, all refer to a zero value or a null value.
Review the following examples:

package main

import "fmt"
func main() { var ptr *int fmt.Printf("ptr 的值为 : %x\n", ptr ) } 

Examples of the above output is:

ptr 的值为 : 0

Analyzing null pointer:

if(ptr != nil)     /* ptr 不是空指针 */
if(ptr == nil) /* ptr 是空指针 */ 

Multiple indirect reference
to the above code as an example, in a: = 3, p1: = & a medium, a point p1 is the memory address, which is called an indirect reference, if there is a point p1 p2 memory address, a point p1 memory address, this is called multiple indirection, no matter what kind of references, if one of the variables to modify the contents of the operation, will affect the other contents of all variables.

func main() {
   a := 3 p1 := &a //p1是指向变量a内存地址的指针 p2 := &p1 //p2是指向变量p1内存地址的指针 fmt.Printf("a:%v, p1:%v, *p1:%v, p2:%v, **p2:%v\n",a,p1,*p1,p2,**p2) a = 4 fmt.Printf("a:%v, p1:%v, *p1:%v, p2:%v, **p2:%v\n",a,p1,*p1,p2,**p2) } -----output----- a:3, p1:0xc0420080b8, *p1:3, p2:0xc042004028, **p2:3 a:4, p1:0xc0420080b8, *p1:4, p2:0xc042004028, **p2:4 

The new function with the operator &
Go language provides two ways to create a variable, and can obtain pointers to them: new function and & operator. The use of these two codes as shown below

type Person struct {
   name string
   sex  string
   age int } func main() { person1 := Person{"zhangsan","man",25} //创建一个person1对象 person2 := new(Person)//使用new创建一个person2对象,同时获得person的指针 person2.name,person2.sex,person2.age = "wangwu","man",25 person3 := &Person{"lisi","man",25}//使用&创建一个person3对象,同时获得person的指针 fmt.Printf("person1:%v, person2:%v, person3:%v\n",person1,person2,person3) } -----output----- person1:{zhangsan man 25}, person2:&{wangwu man 25}, person3:&{lisi man 25} 

From the results, outputs, and new new function & operator in two ways very different, & operators to create more compact, and can always specify the initial value of the attribute. When printing in Go pointer person, the person the specific content of print attributes, and adding to the prefix & showing the variable is a pointer.

Next we pass a structure pointer, and modify the properties of the structure, Go see how language operates.

type Person struct {
   name string
   sex  string
   age int } func main() { person1 := Person{"zhangsan","man",25} //创建一个person1对象 fmt.Printf("person1:%v\n",person1) demo(&person1) fmt.Printf("person1:%v\n",person1) } func demo(person *Person) { (*person).age = 18 //显示的解引用 person.name = "GoLang" //隐式的解引用 } 

Pserson1 objects to create a main function, set the initialization properties after its pointer to demo function, you can see there are two demo function dereference (that is a pointer into the original object) way, the first : (* person) .age solution is shown by reference, using the second operator will automatically dereferencing, both the use of no difference, but the second is simpler. "."



Author: Happy Hour Jill
link: https: //www.jianshu.com/p/af42cb368cef
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/oxspirt/p/10941480.html