This is copied from stack overflow
https://stackoverflow.com/questions/38172661/what-is-the-meaning-of-and
This is possibly one of the most confusing things in Go. There are basically 3 cases you need to understand:
The &
Operator
&
goes in front of a variable when you want to get that variable’s memory address.
The *
Operator
*
goes in front of a variable that holds a memory address and resolves it (it is therefore the counterpart to the &
operator). It goes and gets the thing that the pointer was pointing at, e.g. *myString
.
1 | myString := "Hi" |
or more usefully, something like
1 | myStructPointer = &myStruct |
*
in front of a Type
When *
is put in front of a type, e.g. *string
, it becomes part of the type declaration, so you can say “this variable holds a pointer to a string”. For example:
1 | var str_pointer *string |
So the confusing thing is that the *
really gets used for 2 separate (albeit related) things. The star can be an operator or part of a type.
- Post title:go 指针的问题
- Post author:Yuxuan Wu
- Create time:2021-10-14 08:25:23
- Post link:yuxuanwu17.github.io2021/10/14/2021-10-14-go-指针的问题/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.