使用字符串作为键和字符串或切片作为值的映射?

huangapple go评论92阅读模式
英文:

map with string key and string or slice value?

问题

新手警告。

我可以用字符串作为键和“任何东西”作为值来创建一个地图吗?目标是拥有一个配置数据的地图。这些数据可以是单个字符串(或布尔值或整数,但将其限制为字符串也可以),或者它们可以是字符串数组。例如:我想存储这些项目:

levels = 3
extra-directories = ["foo","bar","baz"]

第一个选项始终是单个值(对我来说,字符串也可以)。第二个选项是零个或多个值。

目标是拥有一个单一的地图,我可以在查看地图时使用switch x.(type)来找出值是什么。

英文:

Newbee warning.

Can I make a map with a string key and "anything" as a value? The goal is to have a map of configuration data. This data can be either a single string (or boolean value or integer, but restricting this to a string is fine), or it can be an array of strings. Example: I'd like to store these items:

levels = 3
extra-directories = ["foo","bar","baz"]

The first option is always a single value (a string would be OK for me). The second option is zero or more values.

The goal is to have a single map where I can store these values and when looking at the map, I could use switch x.(type) to find out what the value is.

答案1

得分: 4

interface{}是一种接受任何类型的类型。

conf := map[string]interface{}{
	"name":    "Default",
	"server":  "localhost",
	"timeout": 120,
}

conf["name"]是一个interface{}而不是一个stringconf["timeout"]是一个interface{}而不是一个int。你可以将conf["name"]传递给接受interface{}类型的函数,比如fmt.Println,但是你不能将其传递给接受string类型的函数,比如strings.ToUpper,除非你知道interface{}的值是一个string(你知道),并断言它的类型:

name := conf["name"].(string)
fmt.Println("name:", strings.ToUpper(name))
server := conf["server"].(string)
fmt.Println("server:", strings.ToUpper(server))
timeout := conf["timeout"].(int)
fmt.Println("timeout in minutes:", timeout/60)

另一种可能适合你问题的解决方案是定义一个结构体:

type Config struct {
	Name    string
	Server  string
	Timeout int
}

创建配置:

conf := Config{
	Name:    "Default",
	Server:  "localhost",
	Tiemout: 60,
}

访问配置:

fmt.Println("name:", strings.ToUpper(conf.Name))
fmt.Println("server:", strings.ToUpper(cnf.Server))
fmt.Println("timeout in minutes:", conf.Timeout/60)
英文:

interface{} is a type that accepts any type.

conf := map[string] interface{} {
	"name": "Default",
	"server": "localhost",
	"timeout": 120,
}

conf["name"] is an interface{} not a string, and conf["timeout"] is an interface{} not an int. You can pass conf["name"] to functions that take interface{} like fmt.Println, but you can't pass it to functions that take string like strings.ToUpper, unless you know that the interface{}'s value is a string (which you do) and assert it's type:

name := conf["name"].(string)
fmt.Println("name:", strings.ToUpper(name))
server := conf["server"].(string)
fmt.Println("server:", strings.ToUpper(server))
timeout := conf["timeout"].(int)
fmt.Println("timeout in minutes:",  timeout / 60)

Another solution that might fit your problem is to define a struct:

type Config struct {
	Name string
	Server string
	Timeout int
}

Create configuration:

conf := Config{
	Name: "Default",
	Server: "localhost",
	Tiemout: 60,
}

Access configuration:

fmt.Println("name:", strings.ToUpper(conf.Name))
fmt.Println("server:", strings.ToUpper(cnf.Server))
fmt.Println("timeout in minutes:",  conf.Timeout / 60)

答案2

得分: 1

是的,您可以使用类型为map[string]interface{}的映射来实现这一点。

英文:

Yes, you can do that using a map having type map[string]interface{}.

huangapple
  • 本文由 发表于 2012年8月4日 02:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/11801346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定