Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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 如何使用值初始化映射_Go - Fatal编程技术网

Go 如何使用值初始化映射

Go 如何使用值初始化映射,go,Go,我有一个多线地图创建和设置的价值观在围棋 类型动物结构{ 食物、运动、声带 } 类型AnimalInterface接口{ 吃 移动() 讲 } animals:=make(映射[字符串]动物) 动物[“牛”]=动物{“草”、“走”、“哞哞”} 动物[“鸟”]=动物{“蠕虫”、“苍蝇”、“窥视”} 动物[“蛇”]=动物{“老鼠”、“滑鼠”、“hsss”} 我更喜欢在一个步骤中创建和初始化。这不管用。。。我该怎么修 animals:=map[string]Animal{ “牛”:动物{“草”、“

我有一个多线地图创建和设置的价值观在围棋

类型动物结构{
食物、运动、声带
}
类型AnimalInterface接口{
吃
移动()
讲
}
animals:=make(映射[字符串]动物)
动物[“牛”]=动物{“草”、“走”、“哞哞”}
动物[“鸟”]=动物{“蠕虫”、“苍蝇”、“窥视”}
动物[“蛇”]=动物{“老鼠”、“滑鼠”、“hsss”}
我更喜欢在一个步骤中创建和初始化。这不管用。。。我该怎么修

animals:=map[string]Animal{
“牛”:动物{“草”、“走”、“哞”}
“鸟”:动物{“蠕虫”、“苍蝇”、“窥视”}
“蛇”:动物{“老鼠”、“滑鼠”、“hsss”}
}
编辑 添加逗号

animals:=map[string]Animal{
“牛”:动物{“草”、“走”、“哞”},
“鸟”:动物{“蠕虫”、“苍蝇”、“窥视”},
“蛇”:动物{“老鼠”、“滑鼠”、“hsss”}
}

打字错误。地图元素后缺少逗号

package main

import (
    "fmt"
)

type Animal struct {
    food, locomotion, sound string
}

type AnimalInterface interface {
    Eat()
    Move()
    Speak()
}

func main() {
    animals := map[string]Animal{
        "cow": Animal{"grass", "walk", "moo"},
        "bird": Animal{"worms", "fly", "peep"},
        "snake": Animal{"mice", "slither", "hsss"},
    }

    fmt.Println(animals)
}

打字错误。地图元素后缺少逗号

package main

import (
    "fmt"
)

type Animal struct {
    food, locomotion, sound string
}

type AnimalInterface interface {
    Eat()
    Move()
    Speak()
}

func main() {
    animals := map[string]Animal{
        "cow": Animal{"grass", "walk", "moo"},
        "bird": Animal{"worms", "fly", "peep"},
        "snake": Animal{"mice", "slither", "hsss"},
    }

    fmt.Println(animals)
}

动物类是什么样的?映射中的每个kvp后面都缺少逗号。谢谢@Sdyess,我添加了类定义。我试着加上逗号,但没有成功。动物类是什么样的?映射中的每个kvp后面都缺少逗号。谢谢@Sdyess,我添加了类定义。我尝试添加逗号,但没有成功。因此,当默认映射时,我们需要在每个值后面添加逗号,即使是最后一个!谢谢因此,在默认映射时,我们需要在每个值后面加一个逗号,即使是最后一个!谢谢