嵌入式和领域的区别

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

Differences of embedded and field

问题

以下是翻译的内容:

这里有一个嵌入了结构体Struct1和一个作为字段定义的结构体Struct2。虽然fmt.Printf()的输出结果相同,但初始化方式有所不同。我对此感到困惑。非常抱歉。

  1. Struct1Struct2之间有什么区别?
  2. 在什么情况下应该使用它们?

代码

  1. type sample1 struct {
  2. Data string
  3. }
  4. type sample2 struct {
  5. Data string
  6. }
  7. type Struct1 struct {
  8. *sample1
  9. *sample2
  10. }
  11. type Struct2 struct {
  12. Sample1 sample1
  13. Sample2 sample2
  14. }
  15. func main() {
  16. s1 := &Struct1{
  17. &sample1{},
  18. &sample2{},
  19. }
  20. s1.sample1.Data = "s1 sample1 data"
  21. s1.sample2.Data = "s1 sample2 data"
  22. s2 := &Struct2{}
  23. s2.Sample1.Data = "s2 sample1 data"
  24. s2.Sample2.Data = "s2 sample2 data"
  25. fmt.Printf("%s, %s\n", s1.sample1.Data, s1.sample2.Data)
  26. fmt.Printf("%s, %s\n", s2.Sample1.Data, s2.Sample2.Data)
  27. }

https://play.golang.org/p/gUy6gwVJDP

非常感谢您的时间和建议。对于我的问题不成熟,我感到非常抱歉。

英文:

There are embedded struct Struct1 and a struct Struct2 defined as field. Both fmt.Printf() are same results while there is a difference for the initialization. I confused about this. I'm sorry.

  1. What differences are these between Struct1 and Struct2?
  2. Which should be used under what situation?

Script

  1. type sample1 struct {
  2. Data string
  3. }
  4. type sample2 struct {
  5. Data string
  6. }
  7. type Struct1 struct {
  8. *sample1
  9. *sample2
  10. }
  11. type Struct2 struct {
  12. Sample1 sample1
  13. Sample2 sample2
  14. }
  15. func main() {
  16. s1 := &Struct1{
  17. &sample1{},
  18. &sample2{},
  19. }
  20. s1.sample1.Data = "s1 sample1 data"
  21. s1.sample2.Data = "s1 sample2 data"
  22. s2 := &Struct2{}
  23. s2.Sample1.Data = "s2 sample1 data"
  24. s2.Sample2.Data = "s2 sample2 data"
  25. fmt.Printf("%s, %s\n", s1.sample1.Data, s1.sample2.Data)
  26. fmt.Printf("%s, %s\n", s2.Sample1.Data, s2.Sample2.Data)
  27. }

https://play.golang.org/p/gUy6gwVJDP

Thank you so much for your time and advices. And I'm sorry for my immature question.

答案1

得分: 1

关于第二个问题,个人而言,我主要使用嵌入来提升方法。

  1. //定义接口
  2. type Doer interface{
  3. Do()
  4. }
  5. //实现接口
  6. func DoWith(d Doer){}
  7. type sample1 struct{}
  8. func (s sample1)Do(){}
  9. type Struct1 struct {
  10. sample1
  11. }
  12. type Struct2 struct {
  13. Sample1 sample1
  14. }
  15. var s1 Struct1
  16. var s2 Struct2
  17. //可以调用
  18. DoWith(s1) //方法被提升,所以满足接口要求
  19. //但是无法调用
  20. DoWith(s2)

关于解码JSON,

  1. type Sample1 struct {
  2. Data string `json:"data"`
  3. }
  4. type Sample2 struct {
  5. Number int `json:"number"`
  6. }
  7. type Struct1 struct {
  8. Sample1
  9. Sample2
  10. }
  11. var s1 Struct1
  12. json.Unmarshal([]byte(`{"data": "foo", "number": 5}`), &s1)
  13. fmt.Println(s1.Data, s1.Number) //输出 foo 5
英文:

As about second question, personally I mostly use embedding to promote methods

  1. //having
  2. type Doer interface{
  3. Do()
  4. }
  5. func DoWith(d Doer){}
  6. func (s sample1)Do(){} //implemented
  7. type Struct1 struct {
  8. sample1
  9. }
  10. type Struct2 struct {
  11. Sample1 sample1
  12. }
  13. var s1 Struct1
  14. var s2 Struct2
  15. //you can call
  16. DoWith(s1) //method promoted so interface satisfied
  17. //but cannot
  18. DoWith(s2)

and to decode JSON,

  1. //having
  2. type Sample1 struct {
  3. Data string `json:"data"`
  4. }
  5. type Sample2 struct {
  6. Number int `json:"number"`
  7. }
  8. //you can easy and handy compose
  9. type Struct1 struct {
  10. Sample1
  11. Sample2
  12. }
  13. var s1 Struct1
  14. json.Unmarshal([]byte(`{"data": "foo", "number": 5}`), &s1)
  15. fmt.Println(s1.Data, s1.Number) //print foo 5

huangapple
  • 本文由 发表于 2017年5月14日 15:09:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/43961290.html
匿名

发表评论

匿名网友

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

确定