【问题标题】:How to literally initialize multi-level nested structs in GO?如何在 GO 中初始化多层嵌套结构?
【发布时间】:2019-01-10 09:51:14
【问题描述】:

我正在尝试在 GO 中初始化以下结构:

这是结构:

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

这是我的代码:

req := &tokenRequest{
    auth: struct {
        identity: struct {
            methods: []string{"password"},
            password: {
                user: {
                    name: os.Username,
                    domain: {
                        id: "default",
                    },
                    password: os.Password,
                },
            },
        },
    },
}

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

我能否在不单独定义所有嵌套结构的情况下进行初始化(即authidentitypassworduser

谢谢。

【问题讨论】:

    标签: go struct composite-literals


    【解决方案1】:

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

    所以改为使用命名的结构类型,这样你就可以像这样在复合文字中使用它们:

    类型:

    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
    }
    

    然后初始化它(在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,
                    },
                },
            },
        },
    }
    

    查看相关问题:Unexpected return of anonymous struct

    【讨论】:

    • 你为什么打字比我快:(
    • @syntaqx 我打赌 icza 构建了一个机器人,可以向他的 HUD 发送通知。他永远是第一位的。
    • 分享?必备! - 哈
    【解决方案2】:

    可以,但你要输入一个lot

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

    你必须告诉 Go 你正在初始化什么类型的变量,所以你只需定义相同的匿名结构,每次都会缓慢但肯定地删除一个级别。

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

    【讨论】:

    • 对低效率进行了很好的可视化。谢谢!
    猜你喜欢
    • 2021-06-22
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多