在结构体中初始化另一个结构体。

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

Initializing a struct in a struct

问题

这是类似帖子的一个小变化。

我有一个名为data的包,其中包含以下内容:

type CityCoords struct {
    Name string
    Lat float64
    Long float64
}

type Country struct {
    Name string
    Capitol *CityCoords
}

在我的主函数中,我尝试这样初始化一个Country:

germany := data.Country {
    Name: "Germany",
    Capitol: {
        Name: "Berlin", // 错误出现在这一行
        Lat: 52.5200,
        Long: 13.4050,
    },
}

当我构建项目时,我得到了与我上面标记的"Name"属性对齐的错误:

missing type in composite literal

如何解决这个错误?

英文:

This is a slight twist on similar posts.

I have a package called data that has the following:

type CityCoords struct {
	Name string
	Lat float64
	Long float64
}

type Country struct {
		Name string
		Capitol *CityCoords
}

In my main function I try initializing a Country like so:

germany := data.Country {
	Name: "Germany",
	Capitol: {
		Name: "Berlin", //error is on this line
		Lat: 52.5200,
		Long: 13.4050,
	},
	
}

When I build my project, I get this error aligned with the "Name" attributed as I've flagged above:

missing type in composite literal

How do I resolve this error?

答案1

得分: 3

据我所知,*表示期望一个对象指针。所以,你可以使用&先初始化它;

func main() {
    germany := &data.Country{
        Name: "Germany",
        Capitol: &data.CityCoords{
            Name: "Berlin", //错误出现在这一行
            Lat: 52.5200,
            Long: 13.4050,
        },
    }
    fmt.Printf("%#v\n", germany)
}

或者,你可以选择一种更优雅的方式;

// data.go
package data

type Country struct {
    Name    string
    Capital *CountryCapital
}

type CountryCapital struct {
    Name    string
    Lat     float64
    Lon     float64
}

func NewCountry(name string, capital *CountryCapital) *Country {
    // 注意:所有属性必须在相同的范围内
    return &Country{name, capital}
}

func NewCountryCapital(name string, lat, lon float64) *CountryCapital {
    // 注意:所有属性必须在相同的范围内
    return &CountryCapital{name, lat, lon}
}

// main.go
func main() {
    c := data.NewCountry("Germany", data.NewCountryCapital("Berlin", 52.5200, 13.4050))
    fmt.Printf("%#v\n", c)
}

以上是翻译好的内容。

英文:

As far as know, * means that an object pointer is expected. So, you could initiate it first using &;

func main() {
    germany := &data.Country{
        Name: "Germany",
        Capitol: &data.CityCoords{
            Name: "Berlin", //error is on this line
            Lat: 52.5200,
            Long: 13.4050,
        },
    }
    fmt.Printf("%#v\n", germany)
}

Or, you can prefer a more elegant way;

// data.go
package data

type Country struct {
    Name    string
    Capital *CountryCapital
}

type CountryCapital struct {
    Name    string
    Lat     float64
    Lon     float64
}

func NewCountry(name string, capital *CountryCapital) *Country {
    // note: all properties must be in the same range
    return &Country{name, capital}
}

func NewCountryCapital(name string, lat, lon float64) *CountryCapital {
    // note: all properties must be in the same range
    return &CountryCapital{name, lat, lon}
}

// main.go
func main() {
    c := data.NewCountry("Germany", data.NewCountryCapital("Berlin", 52.5200, 13.4050))
    fmt.Printf("%#v\n", c)
}

huangapple
  • 本文由 发表于 2016年11月3日 10:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/40393128.html
匿名

发表评论

匿名网友

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

确定