(*variable) and *&variable, what is the difference?

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

(*variable) and *&variable, what is the difference?

问题

我正在使用exercism.com学习GO,并阅读课程大纲中推荐的文档和文章。

现在我正在学习结构体,并找到了下面的代码:

package main

import "fmt"

type Employee struct {
  firstName, lastName string
  salary              int
  fullTime            bool
}

func main() {
  employee := &Employee{
    firstName: "Walddys",
    lastName:  "Dorrejo",
    salary:    1200,
    fullTime:  true,
  }
  fmt.Println("firstName", (*employee).firstName)
}

但是出错了,我输入了fmt.Println("firstName", *&employee.firstName),结果却和之前代码块中使用的结果相同。

我的问题是,使用指针和不使用指针有什么区别吗?

英文:

i am learning GO using exercism.com and reading the recommended documentation and articles from the syllabus.

now i am in struct and found the next code

package main

import "fmt"

type Employee struct {
  firstName, lastName string
  salary              int
  fullTime            bool
}

func main() {
  employee := &Employee{
    firstName: "Walddys",
    lastName:  "Dorrejo",
    salary:    1200,
    fullTime:  true,
  }
  fmt.Println("firstName", (*employee).firstName)
}

but by error, I typed fmt.Println("firstName", *&employee.firstName), which bring me the same result that the previous used in the block of code.

My question is, if exist any different of using this pointer or is the same?

答案1

得分: 3

*& 是多余的语法 - 它获取指向值的指针,然后解引用指针 - 本质上是一个 NOP

如果你安装了静态分析工具,比如 staticcheck,或者使用内置静态分析功能的编辑器,比如 VScode,你会得到以下警告:

*&x 将简化为 x。它不会复制 x。(SA4001)

https://staticcheck.io/docs/checks#SA4001

英文:

*& is redundant syntax - it gets the pointer to the value and then dereferences the pointer - essentially a NOP.

If you install static analysis tools like staticcheck - or use an editor like VScode with built in static analysis - you'll get the following warning:

*&x will be simplified to x. It will not copy x. (SA4001)

https://staticcheck.io/docs/checks#SA4001

答案2

得分: 3

&x 生成指向 x 的指针,而 *p 解引用指针 p。因此,实际上 *& 相互抵消,例如两个语句 v := *&xv := x 是相同的。

这意味着 *&employee.firstNameemployee.firstName 是相同的。

employee 是一个指向结构体的指针,firstName 是该结构体的字段时,表达式 employee.firstName 实际上是 (*employee).firstName 的简写。

这意味着 *&employee.firstName 也等同于 (*employee).firstName


请注意,您应该始终优先使用简写符号。

英文:

&x generates a pointer to x, while *p dereferences the pointer p. So effectively * and & cancel each other out, for example the two statements v := *&x and v := x are identical.

That means that *&employee.firstName is identical to employee.firstName.

And where employee is a pointer to a struct and firstName is a field of that struct, the expression employee.firstName is actually a shorthand for (*employee).firstName.

This means that *&employee.firstName is also identical to (*employee).firstName.


Note that you should always prefer to use the shorthand notation.

huangapple
  • 本文由 发表于 2023年3月21日 05:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75795317.html
匿名

发表评论

匿名网友

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

确定