当前位置:网站首页>Go language pointer, value reference and pointer reference

Go language pointer, value reference and pointer reference

2022-06-24 23:29:00 fenglllle

Preface

Recent projects , Development go Of sdk, Which involves interface programming , similar typescript, The pointer , Pointer types . It's like C Same pronunciation , Pointers involve pointer references .

stay go In language

* type       Indicates that the current type is a pointer type on the type , Represents the value of a pointer on a variable

& Variable       Indicates a pointer to the current variable

demo

Use the structure to simulate :

package main

import "fmt"

type Man struct {
	name string
	age int
}

func sayHello(m Man) string {
	m.name = "not tom"
	return "hello," + m.name
}

func sayHi(m *Man) string {
	m.name = "jim"
	return "hi, " + m.name
}


func main() {
	man := Man{
		name: "tom",
		age: 26,
	}

	fmt.Println(sayHello(man))
	fmt.Println(man.name)
	fmt.Println("---------------------")
	fmt.Println(sayHi(&man))
	fmt.Println(man.name)

}

simulation demo, One uses structural parameters , A parameter that uses a pointer , give the result as follows

 

  You can see that the structure is used to pass , Values modified in the function are not passed outside the function , The structure passed through the pointer type can modify the value of the structure . It can be explained that the parameters of the function are actually copies of the structure of the stack reference , Only modify the pointer , That is, the value of the data structure stored in the memory address can really modify the data .

If the basic data type is used , Results the same , In reflection, the pointer is used to set the value , To modify it successfully , It's the same thing .

contrast Java voice

(138 Bar message ) Java Variable parameters are passed into the method , Whether the external value is affected after modification _fenglllle The blog of -CSDN Blog

The author is in 2018 In writing the Java Whether the modification of the parameter value by the voice method affects the external value , It's actually the same , All roads lead to Rome , however Java The language cannot directly manipulate the pointer of the basic data type , Only pointers to referenced objects can be manipulated , To some extent , A reference object is a pointer , actually Java That's how it's done , Method execution , The stack variable points to the memory address of the heap object .

go The language can directly manipulate pointers of basic data types, and it is also very flexible , This is Java Do not have the ,Java You need to encapsulate the object , It may also be determined by object-oriented design ,go Is interface oriented , The duck type . That is, the same behavior is considered a kind of .

summary

In fact, many phonetics can be used for reference , Sometimes designs come to the same end by different paths , A typical example is the algorithm , Any language , The result of the algorithm is the same .& Address fetch ,* Represents the value of the pointer

原网站

版权声明
本文为[fenglllle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241828224169.html