亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 GO 中逐字初始化多層嵌套結構?

如何在 GO 中逐字初始化多層嵌套結構?

Go
holdtom 2023-06-01 14:22:05
我試圖在 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我可以在不單獨定義所有嵌套結構的情況下進行初始化嗎(即auth, identity, password, user)謝謝。
查看完整描述

2 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

如果你有匿名的、未命名的結構類型,那么如果你重復結構定義,你只能用復合文字初始化它們。這很不方便。


因此,請改用命名結構類型,這樣您就可以像這樣在復合文字中使用它們:


類型:


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,

? ? ? ? ? ? ? ? },

? ? ? ? ? ? },

? ? ? ? },

? ? },

}


查看完整回答
反對 回復 2023-06-01
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

你可以,但你會打很多字:


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 你正在初始化什么類型的變量,所以你只需定義相同的匿名結構,每次緩慢但肯定地刪除一個級別。


出于這個原因,最好使用命名結構,這樣您就不必重復自己。


查看完整回答
反對 回復 2023-06-01
  • 2 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號