结构体类型嵌入字段访问

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

struct type embedded fields access

问题

我正在学习golang,目前我正在尝试理解指针。我定义了三个结构体类型:

  1. type Engine struct {
  2. power int
  3. }
  4. type Tires struct {
  5. number int
  6. }
  7. type Cars struct {
  8. *Engine
  9. Tires
  10. }

如你所见,在Cars结构体中,我定义了一个嵌入类型指针*Engine。看一下main函数:

  1. func main() {
  2. car := new(Cars)
  3. car.number = 4
  4. car.power = 342
  5. fmt.Println(car)
  6. }

当我尝试编译时,我得到了以下错误:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xb code=0x1 addr=0x0 pc=0x23bb]

如何访问power字段呢?

英文:

I am trying to learn golang and at moment I am trying to understand pointers. I defined three struct type

  1. type Engine struct {
  2. power int
  3. }
  4. type Tires struct {
  5. number int
  6. }
  7. type Cars struct {
  8. *Engine
  9. Tires
  10. }

As you can see, in the Cars struct I defined an embedded type pointer *Engine. Look in the main.

  1. func main() {
  2. car := new(Cars)
  3. car.number = 4
  4. car.power = 342
  5. fmt.Println(car)
  6. }

When I try to compile, I've got the following errors

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xb code=0x1 addr=0x0 pc=0x23bb]

How can I access the power field?

答案1

得分: 10

例如,

  1. package main
  2. import "fmt"
  3. type Engine struct {
  4. power int
  5. }
  6. type Tires struct {
  7. number int
  8. }
  9. type Cars struct {
  10. *Engine
  11. Tires
  12. }
  13. func main() {
  14. car := new(Cars)
  15. car.Engine = new(Engine)
  16. car.power = 342
  17. car.number = 4
  18. fmt.Println(car)
  19. fmt.Println(car.Engine, car.power)
  20. fmt.Println(car.Tires, car.number)
  21. }

输出:

  1. &{0x10328100 {4}}
  2. &{342} 342
  3. {4} 4

未经限定的类型名称 EngineTires 充当了各自匿名字段的字段名。

Go编程语言规范

结构体类型

声明了类型但没有显式字段名的字段是匿名字段,也称为嵌入字段或将类型嵌入结构体中。嵌入类型必须指定为类型名 T 或非接口类型名 *T 的指针,并且 T 本身不能是指针类型。未经限定的类型名称充当字段名。

英文:

For example,

  1. package main
  2. import "fmt"
  3. type Engine struct {
  4. power int
  5. }
  6. type Tires struct {
  7. number int
  8. }
  9. type Cars struct {
  10. *Engine
  11. Tires
  12. }
  13. func main() {
  14. car := new(Cars)
  15. car.Engine = new(Engine)
  16. car.power = 342
  17. car.number = 4
  18. fmt.Println(car)
  19. fmt.Println(car.Engine, car.power)
  20. fmt.Println(car.Tires, car.number)
  21. }

Output:

  1. &{0x10328100 {4}}
  2. &{342} 342
  3. {4} 4

The unqualified type names Engine and Tires act as the field names of the respective anonymous fields.

> The Go Programming Language Specification
>
> Struct types
>
> A field declared with a type but no explicit field name is an
> anonymous field, also called an embedded field or an embedding of the
> type in the struct. An embedded type must be specified as a type name
> T or as a pointer to a non-interface type name *T, and T itself may
> not be a pointer type. The unqualified type name acts as the field
> name.

答案2

得分: 8

尝试这样做:

  1. type Engine struct {
  2. power int
  3. }
  4. type Tires struct {
  5. number int
  6. }
  7. type Cars struct {
  8. *Engine
  9. Tires
  10. }
  11. func main() {
  12. car := Cars{&Engine{5}, Tires{10}}
  13. fmt.Println(car.number)
  14. fmt.Println(car.power)
  15. }

如果你想要一个指向Engine的指针,你必须将你的Car结构初始化为:

  1. car := Cars{&Engine{5}, Tires{10}}
  2. fmt.Println(car.number)
  3. fmt.Println(car.power)

你可以在这里查看代码运行的示例:http://play.golang.org/p/_4UFFB7OVI

英文:

Try this:

  1. type Engine struct {
  2. power int
  3. }
  4. type Tires struct {
  5. number int
  6. }
  7. type Cars struct {
  8. Engine
  9. Tires
  10. }

and than:

  1. car := Cars{Engine{5}, Tires{10}}
  2. fmt.Println(car.number)
  3. fmt.Println(car.power)

http://play.golang.org/p/_4UFFB7OVI


If you want a pointer to the Engine, you must initialize your Car structure as:

  1. car := Cars{&Engine{5}, Tires{10}}
  2. fmt.Println(car.number)
  3. fmt.Println(car.power)

答案3

得分: 0

另一个例子,当字段名不唯一时

  1. package embeded
  2. import "fmt"
  3. type Engine struct {
  4. id int
  5. power int
  6. }
  7. type Tires struct {
  8. id int
  9. number int
  10. }
  11. type Cars struct {
  12. id int
  13. Engine
  14. Tires
  15. }
  16. func Embed() Cars {
  17. car := Cars{
  18. id: 3,
  19. Engine: Engine{id: 1, power: 5},
  20. Tires: Tires{id: 2, number: 10},
  21. }
  22. fmt.Println(car.number)
  23. fmt.Println(car.power)
  24. fmt.Println(car.id)
  25. fmt.Println(car.Engine.id)
  26. fmt.Println(car.Tires.id)
  27. return car
  28. }
英文:

One more example, in case of not unique field names

  1. package embeded
  2. import "fmt"
  3. type Engine struct {
  4. id int
  5. power int
  6. }
  7. type Tires struct {
  8. id int
  9. number int
  10. }
  11. type Cars struct {
  12. id int
  13. Engine
  14. Tires
  15. }
  16. func Embed() Cars {
  17. car := Cars{
  18. id: 3,
  19. Engine: Engine{id: 1, power: 5},
  20. Tires: Tires{id: 2, number: 10},
  21. }
  22. fmt.Println(car.number)
  23. fmt.Println(car.power)
  24. fmt.Println(car.id)
  25. fmt.Println(car.Engine.id)
  26. fmt.Println(car.Tires.id)
  27. return car
  28. }

huangapple
  • 本文由 发表于 2014年7月21日 02:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/24853604.html
匿名

发表评论

匿名网友

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

确定