结构体类型嵌入字段访问

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

struct type embedded fields access

问题

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

type Engine struct {
    power int
}

type Tires struct {
    number int
}

type Cars struct {
    *Engine
    Tires
}

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

func main() {
    car := new(Cars)
    car.number = 4
    car.power = 342
    fmt.Println(car)
}

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

panic: runtime error: invalid memory address or nil pointer dereference
[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

type Engine struct {      
	power int             
}                         
                          
type Tires struct {       
	number int            
}                         
                          
                          
type Cars struct {           
	*Engine               
	Tires                 
}       

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

func main() {                    
                                     
	car := new(Cars)             
	car.number = 4               
    car.power = 342     
	fmt.Println(car)            
} 

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

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

How can I access the power field?

答案1

得分: 10

例如,

package main

import "fmt"

type Engine struct {
    power int
}

type Tires struct {
    number int
}

type Cars struct {
    *Engine
    Tires
}

func main() {
    car := new(Cars)
    car.Engine = new(Engine)
    car.power = 342
    car.number = 4
    fmt.Println(car)
    fmt.Println(car.Engine, car.power)
    fmt.Println(car.Tires, car.number)
}

输出:

&{0x10328100 {4}}
&{342} 342
{4} 4

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

Go编程语言规范

结构体类型

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

英文:

For example,

package main

import "fmt"

type Engine struct {
	power int
}

type Tires struct {
	number int
}

type Cars struct {
	*Engine
	Tires
}

func main() {
	car := new(Cars)
	car.Engine = new(Engine)
	car.power = 342
	car.number = 4
	fmt.Println(car)
	fmt.Println(car.Engine, car.power)
	fmt.Println(car.Tires, car.number)
}

Output:

&{0x10328100 {4}}
&{342} 342
{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

尝试这样做:

type Engine struct {
    power int
}

type Tires struct {
    number int
}

type Cars struct {
    *Engine
    Tires
}

func main() {
    car := Cars{&Engine{5}, Tires{10}}
    fmt.Println(car.number)
    fmt.Println(car.power)
}

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

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

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

英文:

Try this:

type Engine struct {      
    power int             
}                         

type Tires struct {       
    number int            
}                         


type Cars struct {           
    Engine               
    Tires                 
}

and than:

car := Cars{Engine{5}, Tires{10}}
fmt.Println(car.number)
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:

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

答案3

得分: 0

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

package embeded

import "fmt"

type Engine struct {
    id    int
    power int
}

type Tires struct {
    id     int
    number int
}

type Cars struct {
    id int
    Engine
    Tires
}

func Embed() Cars {
    car := Cars{
        id:     3,
        Engine: Engine{id: 1, power: 5},
        Tires:  Tires{id: 2, number: 10},
    }
    fmt.Println(car.number)
    fmt.Println(car.power)
    fmt.Println(car.id)
    fmt.Println(car.Engine.id)
    fmt.Println(car.Tires.id)
    return car
}
英文:

One more example, in case of not unique field names

package embeded

import "fmt"

type Engine struct {
	id    int
	power int
}

type Tires struct {
	id     int
	number int
}

type Cars struct {
	id int
	Engine
	Tires
}

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

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:

确定