Skip to content

Instantly share code, notes, and snippets.

@wmydz1
Created March 2, 2018 01:17
Show Gist options
  • Select an option

  • Save wmydz1/f7bb028d02ad9a694b52ad07d4155b5c to your computer and use it in GitHub Desktop.

Select an option

Save wmydz1/f7bb028d02ad9a694b52ad07d4155b5c to your computer and use it in GitHub Desktop.
GoLang 获取变量类型
分类: 实例代码 关键字: reflect type 类型 • golangnote 于 2015-09-05 22:55 发表 • 2533阅
Go 语言可通过reflect 包获取某个变量的类型:
package main
import (
"fmt"
"reflect"
)
func main() {
b := true
s := ""
n := 1
f := 1.0
a := []string{"foo", "bar", "baz"}
fmt.Println(reflect.TypeOf(b), reflect.ValueOf(b).Kind())
fmt.Println(reflect.TypeOf(s), reflect.ValueOf(s).Kind())
fmt.Println(reflect.TypeOf(n), reflect.ValueOf(n).Kind())
fmt.Println(reflect.TypeOf(f), reflect.ValueOf(f).Kind())
fmt.Println(reflect.TypeOf(a), reflect.ValueOf(a).Kind(), reflect.ValueOf(a).Index(0).Kind())
}
输出:
bool bool
string string
int int
float64 float64
[]string slice string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment