Pointer operator -> for golang

huangapple go评论81阅读模式
英文:

Pointer operator -> for golang

问题

似乎golang没有像C和C++那样的指针操作符- >。现在假设我有一个函数,看起来像这样:myfun(myparam * MyType),在函数内部,如果我想访问MyType的成员变量,我必须执行(* myparam).MyMemberVariable。在C和C++中,使用myparam- > MyMemberVariable似乎更容易。

我对go还不太熟悉。不确定我是否遗漏了什么,或者这不是正确的方法?

谢谢。

英文:

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.

答案1

得分: 18

在Go语言中,->.都由.表示。

编译器知道类型,并在必要时进行解引用。

package main

import "fmt"

type A struct {
    X int
}

func main() {
    a0, a1 := A{42}, &A{27}
    fmt.Println(a0.X, a1.X)
}
英文:

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

答案2

得分: 5

你可以使用myparam.MyMemberValue的方式访问成员变量,指针会自动解引用。

根据Go语言规范,选择器会自动解引用指向结构体的指针。如果x是指向结构体的指针,那么x.y就是(x).y的简写;如果字段y也是指向结构体的指针,那么x.y.z就是((x).y).z的简写,依此类推。如果x包含一个类型为A的匿名字段,其中A也是一个结构体类型,那么x.f就是(*x.A).f的简写。

英文:

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.

答案3

得分: 2

嗯,这种自动解引用可能会让人很困惑(对于像我这样的老程序员来说):
1)如果你是用GOLANG学习编程,没问题,这很实用。
2)如果你从C语言转到GOLANG,这会看起来很奇怪,你可能会更喜欢(至少在开始阶段)保留"(*my_var).my_field"表达式而不是"my_var.myfield"。
3)如果你从GOLANG转到C语言,你会遇到很多编译错误。

英文:

Hummm, this automatic dereferencing can be very confusing (for old programmers like me)

  1. If you learn programmation with GOLANG, no problem, it is practical.
  2. If you pass from C to GOLANG, this will seem strange, you will probably prefer (at the beginning at least) to keep the "(*my_var).my_field" expression instead of "my_var.myfield"
  3. If you pass from GOLANG to C, you will get many compilation errors.

答案4

得分: 0

Golang中使用->通过通道传递数据。

package main

import "fmt"

type Person struct {
    Name string
}

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

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

    // 打印对象的默认值
    fmt.Println("无指针", brian.Name)

    // 使用指针访问方法
    brian.printName()

    // 打印结果
    fmt.Println("使用指针", brian.Name)
}
英文:
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)
}

huangapple
  • 本文由 发表于 2014年1月28日 01:48:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/21387955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定