加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在Golang中初始化嵌套结构

发布时间:2020-12-16 09:43:40 所属栏目:大数据 来源:网络整理
导读:我不知道如何初始化一个嵌套的结构。在这里找到一个例子: http://play.golang.org/p/NL6VXdHrjh package maintype Configuration struct { Val string Proxy struct { Address string Port string }}func main() { c := Configuration{ Val: "test",Proxy:
我不知道如何初始化一个嵌套的结构。在这里找到一个例子:
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",},}

}
那么,任何特定的原因,不使代理它自己的结构?

无论如何,你有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)
}

不太适当和丑陋的方式,但仍然工作:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读