如何初始化嵌套结构?

问题描述 投票:0回答:11

我不知道如何初始化嵌套结构。在这里找到一个例子: 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",
        }
    }

}
go
11个回答
265
投票

嗯,有什么具体原因不让 Proxy 成为自己的结构吗?

无论如何,你有2个选择:

正确的方法,只需将 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",
    },
}

182
投票

如果您不想为嵌套结构使用单独的结构定义,并且您不喜欢@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


18
投票

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

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


13
投票

您还可以选择:

type Configuration struct {
        Val string
        Proxy
}

type Proxy struct {
        Address string
        Port    string
}

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

12
投票

您还可以使用

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


9
投票

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

示例:

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"
}

8
投票

您需要在

&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


2
投票

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

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)
}

希望对你有帮助:)


2
投票
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",
        },
    }

}

2
投票

当您的配置是全局的时,您可以这样做:

package main

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

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

0
投票

这是我没有看到提到的另一个选项:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

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

func main() {
    c := Configuration{}
    err := json.Unmarshal([]byte(fmt.Sprintf(`
    {
        "Val": "%s",
        "Proxy": {
            "Address": "%s",
            "Port": "%s"
        }
    }`, "test", "addr", "80")), &c)
    if err != nil {
        log.Fatalf("Error unmarshalling config: %v", err)
    }
}

我不建议您实际这样做,但我发现奇怪的是这是可能的,而问题中的语法却不是。没有技术限制(除了编译器速度),但是 Golang 还没有实现它,所以我们必须使用其他答案之一。考虑到诸如“TypeScript 中始终抽象嵌套类型”之类的文章的存在,这可能是出于风格原因。

© www.soinside.com 2019 - 2024. All rights reserved.