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

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

是否可以使用枚舉索引聲明多維數組?

是否可以使用枚舉索引聲明多維數組?

Go
收到一只叮咚 2023-02-21 19:04:51
我想在 Go 中定義一個二維數組或切片,并使用枚舉值來訪問數組中的數據。以下將不會編譯,我希望你能幫助我弄清楚如何讓它工作。type (    Color  int    Fruit  int    MyType [][]string)const (    Red  Color = 1    Blue Color = 2    Apple     Fruit = 1    BlueBerry Fruit = 2)func DoIt() {    produce := make([][]MyType, 0)      // COMPILE ERROR: "Yummy Apple"' (type string) cannot be represented by the type MyType    produce[Red][Apple] = "Yummy Apple"}
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

是的,可以使用枚舉索引聲明一個數組數組。


    package main

    

    import "fmt"

    

    type (

        Color int

        Fruit int

    )

    

    const (

        Red      Color = 1

        Blue     Color = 2

        NumColor       = 3

    

        Apple     Fruit = 1

        BlueBerry Fruit = 2

        NumFruit        = 3

    )

    

    func main() {

        var produce [NumFruit][NumColor]string

        produce[Red][Apple] = "Yummy Apple"

        fmt.Printf("%#v\n", produce)

    }


https://go.dev/play/p/AxwcxLE3iJX


查看完整回答
反對 回復 2023-02-21
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

從聲明中刪除 MyType。然后它會起作用。


type (

    Color int

    Fruit int

)


const (

    Red  Color = 1

    Blue Color = 2


    Apple     Fruit = 1

    BlueBerry Fruit = 2

)


func DoIt() {

    produce := make([][]string, 0)


    produce[Red][Apple] = "Yummy Apple"

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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