Dictionary 声明并在结构中使用映射
我在戈兰非常不在行,几天前才开始工作 事实上,我正在做一个简单的练习来习惯golang语法 我在main中有这个。go:Dictionary 声明并在结构中使用映射,dictionary,go,struct,type-declaration,golang-migrate,Dictionary,Go,Struct,Type Declaration,Golang Migrate,我在戈兰非常不在行,几天前才开始工作 事实上,我正在做一个简单的练习来习惯golang语法 我在main中有这个。go: package main import( "fmt" // "stringa" "main/survey" ) func main() { var questions = []survey.Question{ { Label: "Questão 1", Instruction
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)
}
我试着做一个简单的结构,但我知道。如果我使用一个接口,问题就解决了,但如果在接下来的步骤中,我需要在结构中使用映射
PS:这是我使用过的一个示例结构:
package survey
import(
// "fmt"
// "strings"
// "strconv"
)
// This is a simple Question in a survey code
type 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
}
最后,问题是:
如何从结构内部使用参数映射并将其写入声明中?必须在结构字段值的复合文字表达式中使用类型(map[int]string
):
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",
},
},
}
复合文字表达式中的类型只能在切片元素(与[]survey.Question
中的元素一样)、映射键和映射值上省略