我在 Go 中创建了多行地图并设置了值。
type Animal struct {
food, locomotion, sound string
}
type AnimalInterface interface {
Eat()
Move()
Speak()
}
animals := make(map[string]Animal)
animals["cow"] = Animal{"grass", "walk", "moo"}
animals["bird"] = Animal{"worms", "fly", "peep"}
animals["snake"] = Animal{"mice", "slither", "hsss"}
我更喜欢一步完成创建和初始化。这不起作用...我应该如何解决它?
animals := map[string]Animal{
"cow": Animal{"grass", "walk", "moo"}
"bird": Animal{"worms", "fly", "peep"}
"snake": Animal{"mice", "slither", "hsss"}
}
编辑 添加逗号
animals := map[string]Animal{
"cow": Animal{"grass", "walk", "moo"},
"bird": Animal{"worms", "fly", "peep"},
"snake": Animal{"mice", "slither", "hsss"}
}
❯ go run animals.go
# command-line-arguments
./animals.go:109:47: syntax error: unexpected newline, expecting comma or }
泛舟湖上清波郎朗
相关分类