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

gee*_*xee 2 struct go composite-literals

我正在尝试从字面上初始化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

谢谢。

icz*_*cza 5

如果您具有匿名的,未命名的结构类型,则只有重复结构定义,才能使用复合文字初始化它们。这很不方便。

因此,请改为使用命名的struct类型,以便您可以在复合文字中使用它们,如下所示:

类型:

type domain struct {
    id string
}

type user struct {
    name     string
    domain   domain
    password string
}

type password struct {
    user user
}

type identity struct {
    methods  []string
    password password
}

type auth struct {
    identity identity
}

type tokenRequest struct {
    auth auth
}
Run Code Online (Sandbox Code Playgroud)

然后将其初始化(在Go Playground上尝试):

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

请参阅相关问题:匿名结构的意外返回

  • 你为什么打字比我快:( (2认同)

syn*_*aqx 5

可以,但你会打字一个不少

package main

import (
    "fmt"
    "net/http"
)

type tokenRequest struct {
    auth struct {
        identity struct {
            methods  []string
            password struct {
                user struct {
                    name   string
                    domain struct {
                        id string
                    }
                    password string
                }
            }
        }
    }
}

func main() {
    s := tokenRequest{
        auth: struct {
            identity struct {
                methods  []string
                password struct {
                    user struct {
                        name   string
                        domain struct {
                            id string
                        }
                        password string
                    }
                }
            }
        }{
            identity: struct {
                methods  []string
                password struct {
                    user struct {
                        name   string
                        domain struct {
                            id string
                        }
                        password string
                    }
                }
            }{
                methods: []string{http.MethodGet, http.MethodPost},
                password: struct {
                    user struct {
                        name   string
                        domain struct {
                            id string
                        }
                        password string
                    }
                }{
                    user: struct {
                        name   string
                        domain struct {
                            id string
                        }
                        password string
                    }{
                        name: "username",
                        domain: struct {
                            id string
                        }{
                            id: "domain id",
                        },
                        password: "password",
                    },
                },
            },
        },
    }

    fmt.Printf("%+v\n", s)
}
Run Code Online (Sandbox Code Playgroud)

您必须告诉Go您要初始化的变量类型,因此您只需定义相同的匿名结构即可,但每次都要缓慢但确定地删除一个级别。

因此,最好使用命名结构,这样您就不必重复自己了。

  • 低效率的很好的可视化。谢谢! (2认同)