douping3891 2019-01-10 09:51
浏览 176
已采纳

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

I am trying to literally initialize the following struct in GO:

This is the struct:

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

And this is my code:

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

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

Thank you.

  • 写回答

2条回答 默认 最新

  • duanqi5114 2019-01-10 10:00
    关注

    You can, but you're going to be typing a 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
    ", s)
    }
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏