如何在结构体的方法中设置和获取字段

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

How to set and get fields in struct's method

问题

创建一个这样的结构体之后:

  1. type Foo struct {
  2. name string
  3. }
  4. func (f Foo) SetName(name string) {
  5. f.name = name
  6. }
  7. func (f Foo) GetName() string {
  8. return f.name
  9. }

我该如何创建一个新的Foo实例并设置和获取name?
我尝试了以下代码:

  1. p := new(Foo)
  2. p.SetName("Abc")
  3. name := p.GetName()
  4. fmt.Println(name)

没有输出任何内容,因为name是空的。那么我该如何设置和获取结构体内的字段呢?

工作示例

英文:

After creating a struct like this:

  1. type Foo struct {
  2. name string
  3. }
  4. func (f Foo) SetName(name string) {
  5. f.name = name
  6. }
  7. func (f Foo) GetName() string {
  8. return f.name
  9. }

How do I create a new instance of Foo and set and get the name?
I tried the following:

  1. p := new(Foo)
  2. p.SetName("Abc")
  3. name := p.GetName()
  4. fmt.Println(name)

Nothing gets printed, because name is empty. So how do I set and get a field inside a struct?

Working playground

答案1

得分: 143

包括注释(和工作)的示例:

  1. package main
  2. import "fmt"
  3. type Foo struct {
  4. name string
  5. }
  6. // SetName 接收一个指向 Foo 的指针,以便修改它。
  7. func (f *Foo) SetName(name string) {
  8. f.name = name
  9. }
  10. // Name 接收 Foo 的一个副本,因为它不需要修改它。
  11. func (f Foo) Name() string {
  12. return f.name
  13. }
  14. func main() {
  15. // 注意 Foo{}。new(Foo) 只是 &Foo{} 的语法糖,
  16. // 我们不需要一个指向 Foo 的指针,所以我替换了它。
  17. // 不过与问题无关。
  18. p := Foo{}
  19. p.SetName("Abc")
  20. name := p.Name()
  21. fmt.Println(name)
  22. }

测试它 并参考 A Tour of Go 了解更多关于方法和指针以及 Go 的基础知识。

英文:

Commentary (and working) example:

  1. package main
  2. import "fmt"
  3. type Foo struct {
  4. name string
  5. }
  6. // SetName receives a pointer to Foo so it can modify it.
  7. func (f *Foo) SetName(name string) {
  8. f.name = name
  9. }
  10. // Name receives a copy of Foo since it doesn't need to modify it.
  11. func (f Foo) Name() string {
  12. return f.name
  13. }
  14. func main() {
  15. // Notice the Foo{}. The new(Foo) was just a syntactic sugar for &Foo{}
  16. // and we don't need a pointer to the Foo, so I replaced it.
  17. // Not relevant to the problem, though.
  18. p := Foo{}
  19. p.SetName("Abc")
  20. name := p.Name()
  21. fmt.Println(name)
  22. }

Test it and take A Tour of Go to learn more about methods and pointers, and the basics of Go at all.

答案2

得分: 34

设置器和获取器在Go语言中并不是那么惯用的。特别是字段x的获取器不是命名为GetX,而是直接命名为X。请参考http://golang.org/doc/effective_go.html#Getters。

如果设置器没有提供特殊逻辑,例如验证逻辑,那么导出字段并且不提供设置器和获取器方法也没有问题。(对于有Java背景的人来说,这可能感觉不对。但实际上是可以的。)

英文:

Setters and getters are not that idiomatic to Go.
Especially the getter for a field x is not named GetX
but just X.
See http://golang.org/doc/effective_go.html#Getters

If the setter does not provide special logic, e.g.
validation logic, there is nothing wrong with exporting
the field and neither providing a setter nor a getter
method. (This just feels wrong for someone with a
Java background. But it is not.)

答案3

得分: 7

例如,

  1. package main
  2. import "fmt"
  3. type Foo struct {
  4. name string
  5. }
  6. func (f *Foo) SetName(name string) {
  7. f.name = name
  8. }
  9. func (f *Foo) Name() string {
  10. return f.name
  11. }
  12. func main() {
  13. p := new(Foo)
  14. p.SetName("Abc")
  15. name := p.Name()
  16. fmt.Println(name)
  17. }

输出:

  1. Abc
英文:

For example,

  1. package main
  2. import "fmt"
  3. type Foo struct {
  4. name string
  5. }
  6. func (f *Foo) SetName(name string) {
  7. f.name = name
  8. }
  9. func (f *Foo) Name() string {
  10. return f.name
  11. }
  12. func main() {
  13. p := new(Foo)
  14. p.SetName("Abc")
  15. name := p.Name()
  16. fmt.Println(name)
  17. }

Output:

  1. Abc

huangapple
  • 本文由 发表于 2012年8月5日 00:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/11810218.html
匿名

发表评论

匿名网友

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

确定