Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为go(golang)中的切片结构赋值_Go - Fatal编程技术网

为go(golang)中的切片结构赋值

为go(golang)中的切片结构赋值,go,Go,如何为[]struct类型的变量赋值 type Mappings []struct { PropA string PropB string } func main() { var test Mappings test = ??? } 提前谢谢 非常感谢!我在这上面浪费了好几个小时;是后面的逗号吸引了我!速记test:=Mappings{{{“France”,“Paris”},{“Italy”:“Rome”}不,这不起作用,需要te

如何为[]struct类型的变量赋值

type Mappings []struct {
    PropA       string  
    PropB       string 
}

func main() {
    var test Mappings
    test = ???
}

提前谢谢

非常感谢!我在这上面浪费了好几个小时;是后面的逗号吸引了我!速记
test:=Mappings{{{“France”,“Paris”},{“Italy”:“Rome”}
不,这不起作用,需要
test:=Mappings{{“France”,“Paris”},{“Italy”:“Rome”},}
@shazbot只有在使用换行符时才需要尾随逗号,因为Go会隐式添加一个
(语句终止符)到不是以某些标记结尾的每一行的结尾。非常感谢!我在这上面浪费了好几个小时;是后面的逗号吸引了我!速记
test:=Mappings{{{“France”,“Paris”},{“Italy”:“Rome”}
不,这不起作用,需要
test:=Mappings{{“France”,“Paris”},{“Italy”:“Rome”},}
@shazbot只有在使用换行符时才需要尾随逗号,因为Go会隐式添加一个
(语句终止符)到不是以某些标记结尾的每一行的结尾。见
package main

import (
    "fmt"
)
type Mappings []struct {
    PropA       string  
    PropB       string 
}

func main() {
    var test Mappings
    test = Mappings{
        {PropA: "foo", PropB: "bar"},
        {PropA: "bar", PropB: "baz"},
    }
    fmt.Println(test)
}