What is the meaning of '*' and '&'?

前端 未结 5 1064
情歌与酒
情歌与酒 2021-01-30 09:07

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

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 09:38

    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.

提交回复
热议问题