指针运算符->用于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.

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)
}

You can do myparam.MyMemberValue, pointers are automatically dereferenced

Go spec:

Selectors automatically dereference pointers to structs. If x is a pointer to a struct, x.y is shorthand for (x).y; if the field y is also a pointer to a struct, x.y.z is shorthand for ((*x).y).z, and so on. If x contains an anonymous field of type *A, where A is also a struct type, x.f is shorthand for (*x.A).f.

Goes uses -> for passing data by using channels.

package main

import "fmt"

type Person struct {
    Name string
}

func (p *Person) printName() {
    p.Name = "brian"
}

func main() {
    // obj
    brian := Person{""}

    // prints obj default value
    fmt.Println("No pointer", brian.Name) 

    // it access the method with the pointer
    brian.printName()

    // prints the result 
    fmt.Println("With a pointer", brian.Name)
}