I am doing the http://tour.golang.org/. Could anyone explain me lines 1,3,5 and 7 this function especially what '*' and '&' do? I mean by mentioning them in a function declaration, what they are supposed/expected to do? A toy example:
1: func intial1(var1 int, var2 int, func1.newfunc[]) *callproperfunction { 2: 3: addition:= make ([] add1, var1) 4: for i:=1;i<var2;i++ { 5: var2 [i] = *addtother (randomstring(lengthofcurrent)) 6: } 7: return &callproperfunction {var1 int, var2 int, func1.newfunc[], jackpot} 8: }
It seems that they are pointers like what we have in C++. But I cannot connect those concepts to what we have here. In other words, what '*' an '&' do when I use them in function declaration in Go.
I know what reference and dereference mean. What I cannot understand is: how we can use pointer to a function is Go. for example line 1 and 7, what these two lines do? Function named intial1 is declared that returns a pointer? and in line 7, we call it with arguments using return function.
asterisk • \ASS-tuh-risk\ • noun. : the character * used in printing or writing as a reference mark, as an indication of the omission of letters or words, to denote a hypothetical or unattested linguistic form, or for various arbitrary meanings.
In English, the symbol * is generally called asterisk. Depending on the context, the asterisk symbol has different meanings. In Math, for instance, the asterisk symbol is used for multiplication of two numbers, let's say 4 * 5; in this case, the asterisk is voiced 'times,' making it “4 times 5”.
& in C is only an operator, it yields the address (or pointer to) of an object. It cannot be used in a declaration. In C++ it is a type qualifier for a reference which is similar to a pointer but has more restrictive behaviour and is therefore often safer.
Word forms: asterisks An asterisk is the sign *. It is used especially to indicate that there is further information about something in another part of the text.
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
.
myString := "Hi" fmt.Println(*&myString) // prints "Hi"
or more usefully, something like
myStructPointer = &myStruct // ... (*myStructPointer).someAttribute = "New Value"
*
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With