In go, is this line creating a new empty map with string as key and value?

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

In go, is this line creating a new empty map with string as key and value?

问题

这行代码是在Go语言中创建一个新的空映射(map),其中键和值都是字符串类型。

英文:
  1. keys := []string{}

In go, is this line creating a new empty map with string as key and value?

答案1

得分: 5

不,那是创建一个空字符串切片。

这是一个键和值都为字符串类型的空映射:

  1. keys := map[string]string{}
英文:

No, that's creating an empty string slice.

This is an empty map with string as key and value:

  1. keys := map[string]string{}

答案2

得分: 2

如果你不知道类型是什么,你可以使用reflect包进行检查:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. keys := []string{}
  8. fmt.Println(reflect.TypeOf(keys))
  9. fmt.Println(reflect.TypeOf(keys).Elem())
  10. }

输出结果:

  1. []string
  2. string

这表示一个空的字符串切片。

另一个例子是检查切片中的类型:

  1. keys := []string{}
  2. anotherKeys := []map[string]string{}
  3. fmt.Println("keys: ", reflect.TypeOf(keys).Elem())
  4. fmt.Println("anotherKeys: ", reflect.TypeOf(anotherKeys).Elem())

输出结果:

  1. keys: string
  2. anotherKeys: map[string]string

注意:
Elem()

英文:

If you don't know what is the type, you can always check using reflect package:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. keys := []string{}
  8. fmt.Println(reflect.TypeOf(keys))
  9. fmt.Println(reflect.TypeOf(keys).Elem())
  10. }

Output:

  1. []string
  2. string

Which means an empty slice of string.

Another eg to check the types inside the slice:

  1. keys := []string{}
  2. anotherKeys := []map[string]string{}
  3. fmt.Println("keys: ", reflect.TypeOf(keys).Elem())
  4. fmt.Println("anotherKeys: ", reflect.TypeOf(anotherKeys).Elem())

Output:

  1. keys: string
  2. anotherKeys: map[string]string

NB:
Elem()

答案3

得分: 0

不,[]string 是一个字符串切片。

如果你想要一个键和值都是字符串类型的空映射,那么 map[string]string 就是正确的类型。在 Playground 中尝试以下示例代码,你就可以看到结果:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func printKind(i interface{}) {
  7. fmt.Printf("Kind of %#v: %s\n", i, reflect.TypeOf(i).Kind())
  8. }
  9. func main() {
  10. emptySlice := []string{}
  11. printKind(emptySlice)
  12. emptyMap := map[string]string{}
  13. printKind(emptyMap)
  14. }
英文:

No, []string is a slice of strings.

If you want an empty map with strings as the type for both keys and values, then map[string]string would be the correct type. Try the following example code in the Playground to see it for yourself:

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func printKind(i interface{}) {
  7. fmt.Printf("Kind of %#v: %s\n", i, reflect.TypeOf(i).Kind())
  8. }
  9. func main() {
  10. emptySlice := []string{}
  11. printKind(emptySlice)
  12. emptyMap := map[string]string{}
  13. printKind(emptyMap)
  14. }

huangapple
  • 本文由 发表于 2016年12月3日 08:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/40942784.html
匿名

发表评论

匿名网友

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

确定