相关疑难解决方法(0)

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

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

go

108
推荐指数
7
解决办法
12万
查看次数

如何从字面上初始化GO中的多层嵌套结构?

我正在尝试从字面上初始化GO中的以下结构:

这是结构:

type tokenRequest struct {
    auth struct {
        identity struct {
            methods  []string
            password struct {
                user struct {
                    name   string
                    domain struct {
                        id string
                    }
                    password string
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

req := &tokenRequest{
    auth: struct {
        identity: struct {
            methods: []string{"password"},
            password: {
                user: {
                    name: os.Username,
                    domain: {
                        id: "default",
                    },
                    password: os.Password,
                },
            },
        },
    },
}
Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/e8Yuk-37_nN

我可以初始化,而无需单独定义所有嵌套的结构(即authidentitypassworduser

谢谢。

struct go composite-literals

2
推荐指数
2
解决办法
368
查看次数

标签 统计

go ×2

composite-literals ×1

struct ×1