Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer operator -> for golang

It seems golang does not have the pointer operator -> as C and C++ have. Now let's say I have a function looks something like this: myfun(myparam *MyType), inside the function, if I want to access the member variables of MyType, I have to do (*myparam).MyMemberVariable. It seems to be a lot easier to do myparam->MyMemberVariable in C and C++.

I'm quite new to go. Not sure if I'm missing something, or this is not the right way to go?

Thanks.

like image 806
Qian Chen Avatar asked Jan 27 '14 17:01

Qian Chen


1 Answers

In Go, both -> and . are represented by .

The compiler knows the types, and can dereference if necessary.

package main

import "fmt"

type A struct {
    X int
}

func main() {
    a0, a1 := A{42}, &A{27}
    fmt.Println(a0.X, a1.X)
}
like image 56
Paul Hankin Avatar answered Oct 01 '22 02:10

Paul Hankin