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 the
Those are pointers like we have in C++.
The differences are:
Instead of ->
to call a method on a pointer, you always use .
, i.e. pointer.method()
.
There are no dangling pointers. It is perfectly valid to return a pointer to a local variable. Golang will ensure the lifetime of the object and garbage-collect it when it's no longer needed.
Pointers can be created with new()
or by creating a object object{}
and taking the address of it with &
.
Golang does not allow pointer-arithmetic (arrays do not decay to pointers) and insecure casting. All downcasts will be checked using the runtime-type of the variable and either panic
or return false as second return-value when the instance is of the wrong type, depending on whether you actually take the second return type or not.