在Golang中初始化一个嵌套的结构

son*_*ags 108 go

我无法弄清楚如何初始化嵌套结构.在这里找一个例子: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",
        },
    }

}
Run Code Online (Sandbox Code Playgroud)

One*_*One 153

那么,任何特定的理由都不能使代理自己的结构?

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

正确的方法,只需将代理移动到自己的结构,例如:

type Configuration struct {
    Val string
    Proxy
}

type Proxy struct {
    Address string
    Port    string
}

func main() {

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

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

c := &Configuration{
    Val: "test",
    Proxy: struct {
        Address string
        Port    string
    }{
        Address: "addr",
        Port:    "80",
    },
}
Run Code Online (Sandbox Code Playgroud)

  • 在第二种方法中,我们可以避免重复的struct定义吗? (3认同)
  • 如果代理有一个类型为 struct 的字段怎么办?如何在另一个嵌套结构中初始化它们? (2认同)
  • 第一个提议导致名称空间冲突。您必须使用“ConfigurationProxy”而不是“Proxy”来避免冲突。如果你做得正确,你会发现这对于每个嵌套级别都会变得更烦人。 (2认同)

sep*_*ehr 77

如果您不想为嵌套结构使用单独的结构定义,并且您不喜欢@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`
}
Run Code Online (Sandbox Code Playgroud)

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

  • 哇,关于如何初始化**嵌套**结构的问题的实际答案. (5认同)

Vit*_*rio 14

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",
    },
}
Run Code Online (Sandbox Code Playgroud)

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

  • 如果“P Proxy”是一个数组怎么办? (3认同)

Jos*_*ose 10

你也有这个选项:

type Configuration struct {
        Val string
        Proxy
}

type Proxy struct {
        Address string
        Port    string
}

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


Fer*_*uis 9

您还可以new手动分配 using和初始化所有字段

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"
}
Run Code Online (Sandbox Code Playgroud)

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


dvd*_*plm 7

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

例:

package animals

type otherProps{
  Name string
  Width int
}

type Duck{
  Weight int
  otherProps
}
Run Code Online (Sandbox Code Playgroud)

你如何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"
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您需要在此期间重新定义未命名的结构 &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)
}
Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/Fv5QYylFGAY