How to check variable type is map in Go language

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

How to check variable type is map in Go language

问题

在Go语言中,你可以使用反射(reflection)来检查变量的类型是否为map。你可以使用reflect包中的TypeOf函数获取变量的类型,并使用Kind方法判断类型是否为map。下面是一个示例代码:

import (
    "fmt"
    "reflect"
)

func main() {
    map1 := map[string]string{"name":"John","desc":"Golang"}
    map2 := map[string]int{"apple":23,"tomato":13}

    // 检查map1的类型
    if reflect.TypeOf(map1).Kind() == reflect.Map {
        fmt.Println("map1 is a map")
    } else {
        fmt.Println("map1 is not a map")
    }

    // 检查map2的类型
    if reflect.TypeOf(map2).Kind() == reflect.Map {
        fmt.Println("map2 is a map")
    } else {
        fmt.Println("map2 is not a map")
    }
}

运行以上代码,输出结果将显示map1是一个map,而map2也是一个map。

英文:
map1 := map[string]string{"name":"John","desc":"Golang"}
map2 := map[string]int{"apple":23,"tomato":13}

so,How to check variable type is map in Go language?

答案1

得分: 19

你可以使用reflect.ValueOf()函数来获取这些映射的值,然后从值中获取类型,其中包含一个Map条目(reflect.Map)。

这是一个更具体的示例,使用reflect.Map进行比较:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	map1 := map[string]string{"name": "John", "desc": "Golang"}
	map2 := map[string]int{"apple": 23, "tomato": 13}
	slice1 := []int{1,2,3}
	fmt.Printf("%v is a map? %v\n", map1, reflect.ValueOf(map1).Kind() == reflect.Map)
	fmt.Printf("%v is a map? %v\n", map2, reflect.ValueOf(map2).Kind() == reflect.Map)
	fmt.Printf("%v is a map? %v\n", slice1, reflect.ValueOf(slice1).Kind() == reflect.Map)
}

输出结果为:

map[name:John desc:Golang] is a map? true
map[apple:23 tomato:13] is a map? true
[1 2 3] is a map? false

如果你想知道更具体的映射类型,可以使用reflect.TypeOf()

fmt.Println(reflect.TypeOf(map1))
fmt.Println(reflect.TypeOf(map2))
fmt.Println(reflect.TypeOf(slice1))

希望对你有帮助!

英文:

You can use the reflect.ValueOf() function to get the Value of those maps, and then get the Kind from the Value, which has a Map entry (reflect.Map).

http://play.golang.org/p/5AUKxECqNA

http://golang.org/pkg/reflect/#Kind

Here's a more specific example that does the comparison with reflect.Map:
http://play.golang.org/p/-qr2l_6TDq

package main

import (
   "fmt"
   "reflect"
)

func main() {
   map1 := map[string]string{"name": "John", "desc": "Golang"}
   map2 := map[string]int{"apple": 23, "tomato": 13}
   slice1 := []int{1,2,3}
   fmt.Printf("%v is a map? %v\n", map1, reflect.ValueOf(map1).Kind() == reflect.Map)
   fmt.Printf("%v is a map? %v\n", map2, reflect.ValueOf(map2).Kind() == reflect.Map)
   fmt.Printf("%v is a map? %v\n", slice1, reflect.ValueOf(slice1).Kind() == reflect.Map)
}

prints:

map[name:John desc:Golang] is a map? true
map[apple:23 tomato:13] is a map? true
[1 2 3] is a map? false

If you want to know the more specific map type, you can use reflect.TypeOf():

http://play.golang.org/p/mhjAAdgrG4

答案2

得分: -1

你可以使用fmt包来避免使用反射。

func isMap(x interface{}) bool {
    t := fmt.Sprintf("%T", x)
    return strings.HasPrefix(t, "map[")
}

map1 := map[string]string{"name": "John", "desc": "Golang"}
fmt.Printf("map1 is a map? %t", isMap(map1))

输出结果为:

map1 is a map? true

反射在Go语言中是不被鼓励的。引用Rob Pike的话说,"清晰比聪明更好",而"反射从来都不清晰"。

英文:

You can use fmt and avoid using reflection.

func isMap(x interface{}) bool {
    t := fmt.Sprintf("%T", x)
	return strings.HasPrefix(t, "map[")
}

map1 := map[string]string{"name": "John", "desc": "Golang"}
fmt.Printf("map1 is a map? %t", isMap(map1))

Produces:

map1 is a map? true

https://play.golang.org/p/OVm-_jgz33r

Reflection is discouraged in Go. To quote Rob Pike,
"Clear is better than clever" and "Reflection is never clear."

huangapple
  • 本文由 发表于 2013年12月24日 19:05:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/20759803.html
匿名

发表评论

匿名网友

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

确定