我對(duì) golang 非常菜鳥,幾天前才開始。實(shí)際上我正在嘗試做一個(gè)簡(jiǎn)單的練習(xí)來習(xí)慣 golang 語法。我在 main.go 中有這個(gè):package main import( "fmt" // "stringa" "main/survey")func main() { var questions = []survey.Question{ { Label: "Quest?o 1", Instructions : "Instru??o", Options : { 1 : "Op1", 2 : "Op2", }, Answer: { 1 : "Op1", }, }, } fmt.Println(questions[0].Label)}我嘗試制作一個(gè)簡(jiǎn)單的結(jié)構(gòu),但我知道。如果我使用接口,問題就可以解決,但如果在接下來的步驟中,我將需要在結(jié)構(gòu)中使用地圖......PS:這是我使用過的示例結(jié)構(gòu):package surveyimport( // "fmt" // "strings" // "strconv")// This is a simple Question in a survey codetype Question struct { // This is a label for the quetsion Label string // This is a instructions and is not required Instructions string // this is a multiple options answer Options map[int]string // this is a answer correct response Answer map[int]string}最后的問題是:如何在結(jié)構(gòu)內(nèi)部的參數(shù)中使用映射并將其寫入聲明中?
1 回答

白衣非少年
TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
類型 ( map[int]string) 必須在結(jié)構(gòu)體字段值的復(fù)合文字表達(dá)式中使用:
var questions = []survey.Question{
{
Label: "Quest?o 1",
Instructions: "Instru??o",
Options: map[int]string{
1: "Op1",
2: "Op2",
},
Answer: map[int]string{
1: "Op1",
},
},
}
復(fù)合文字表達(dá)式中的類型只能在切片元素(與 的元素一樣[]survey.Question)、映射鍵和映射值上被省略。
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報(bào)
0/150
提交
取消