如何在 GO 中逐字初始化多级嵌套结构? [英] How to literally initialize multi-level nested structs in GO?

查看:35
本文介绍了如何在 GO 中逐字初始化多级嵌套结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 GO 中逐字初始化以下结构:

I am trying to literally initialize the following struct in 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)

Can I initialize without defining all the nested structs separately (i.e. auth, identity, password, user)

谢谢.

推荐答案

可以,但你要输入:

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 你正在初始化什么类型的变量,所以你只需定义相同的匿名结构,每次缓慢但肯定地删除一个级别.

You have to tell Go what type of variable you're initializing, so you just define the same anonymous struct, slowly but surely removing a level each time.

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

For this reason, it's better to use named structs so you don't have to repeat yourself.

这篇关于如何在 GO 中逐字初始化多级嵌套结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆