如何初始化嵌套结构?

我不知道如何初始化嵌套结构: Http://play.golang.org/p/nl6vxdhrjh

package main


type Configuration struct {
Val   string
Proxy struct {
Address string
Port    string
}
}


func main() {


c := &Configuration{
Val: "test",
Proxy: {
Address: "addr",
Port:    "80",
}
}


}
210539 次浏览

Configuration之外单独定义 Proxy结构,如下所示:

type Proxy struct {
Address string
Port    string
}


type Configuration struct {
Val string
P   Proxy
}


c := &Configuration{
Val: "test",
P: Proxy{
Address: "addr",
Port:    "80",
},
}

参见 http://play.golang.org/p/7PELCVsQIc

有什么特别的原因不让 Proxy 成为它自己的结构吗?

不管怎样,你有两个选择:

正确的方法是将代理移动到它自己的结构中,例如:

type Configuration struct {
Val string
Proxy Proxy
}


type Proxy struct {
Address string
Port    string
}


func main() {


c := &Configuration{
Val: "test",
Proxy: Proxy{
Address: "addr",
Port:    "port",
},
}
fmt.Println(c)
fmt.Println(c.Proxy.Address)
}

不那么恰当和丑陋的方式,但仍然有效:

c := &Configuration{
Val: "test",
Proxy: struct {
Address string
Port    string
}{
Address: "addr",
Port:    "80",
},
}

你还可以选择:

type Configuration struct {
Val string
Proxy
}


type Proxy struct {
Address string
Port    string
}


func main() {
c := &Configuration{"test", Proxy{"addr", "port"}}
fmt.Println(c)
}

当您希望实例化在外部包中定义的公共类型并且该类型嵌入其他私有类型时,会出现一个问题。

例如:

package animals


type otherProps{
Name string
Width int
}


type Duck{
Weight int
otherProps
}

如何在自己的程序中实例化 Duck? 下面是我能想到的最好方法:

package main


import "github.com/someone/animals"


func main(){
var duck animals.Duck
// Can't instantiate a duck with something.Duck{Weight: 2, Name: "Henry"} because `Name` is part of the private type `otherProps`
duck.Weight = 2
duck.Width = 30
duck.Name = "Henry"
}

如果你不想为嵌套的 struct 使用单独的 struct 定义,或者你不喜欢@OneOfOne 建议的第二个方法,你可以使用第三个方法:

package main
import "fmt"
type Configuration struct {
Val   string
Proxy struct {
Address string
Port    string
}
}


func main() {
c := &Configuration{
Val: "test",
}


c.Proxy.Address = `127.0.0.1`
c.Proxy.Port = `8080`
}

你可以在这里检查: https://play.golang.org/p/WoSYCxzCF2

您可以定义一个 struct 并在另一个 struct 中创建它的对象,就像我在下面所做的那样:

package main


import "fmt"


type Address struct {
streetNumber int
streetName   string
zipCode      int
}


type Person struct {
name    string
age     int
address Address
}


func main() {
var p Person
p.name = "Vipin"
p.age = 30
p.address = Address{
streetName:   "Krishna Pura",
streetNumber: 14,
zipCode:      475110,
}
fmt.Println("Name: ", p.name)
fmt.Println("Age: ", p.age)
fmt.Println("StreetName: ", p.address.streetName)
fmt.Println("StreeNumber: ", p.address.streetNumber)
}

希望对你有所帮助:)

您需要在 &Configuration{}期间重新定义未命名结构

package main


import "fmt"


type Configuration struct {
Val   string
Proxy struct {
Address string
Port    string
}
}


func main() {


c := &Configuration{
Val: "test",
Proxy: struct {
Address string
Port    string
}{
Address: "127.0.0.1",
Port:    "8080",
},
}
fmt.Println(c)
}

Https://play.golang.org/p/fv5qyylfgay

您还可以使用 new进行分配,并手动初始化所有字段

package main


type Configuration struct {
Val   string
Proxy struct {
Address string
Port    string
}
}


func main() {
c := new(Configuration)
c.Val = "test"
c.Proxy.Address = "addr"
c.Proxy.Port = "80"
}

看操场: https://play.golang.org/p/sFH_-HawO_M

package main


type    Proxy struct {
Address string
Port    string
}


type Configuration struct {
Proxy
Val   string


}


func main() {


c := &Configuration{
Val: "test",
Proxy: Proxy {
Address: "addr",
Port:    "80",
},
}


}