原始类型

让我们来看看可以对int或string这种原始类型做什么操作。

获取类型

package main

import (
	"fmt"
	"reflect"
)

func printType(v interface{}) {
	rv := reflect.ValueOf(v)
	typ := rv.Type()
	typeName := ""
	switch rv.Kind() {
	case reflect.Ptr:
		typeName = "pointer"
	case reflect.Int:
		typeName = "int"
	case reflect.Int32:
		typeName = "int32"
	case reflect.String:
		typeName = "string"
	// ... handle more cases
	default:
		typeName = "unrecognized type"
	}
	fmt.Printf("v is of type '%s'. Size: %d bytes\n", typeName, typ.Size())
}

func main() {
	printType(int32(3))
	printType("")
	i := 3
	printType(&i) // *int i.e. pointer to int
}

/*
---------OUTPUT---------
v is of type 'int32'. Size: 4 bytes
v is of type 'string'. Size: 16 bytes
v is of type 'pointer'. Size: 8 bytes
*/

在实际代码中,你可以处理你关心的所有类型

获取值

为了最小化API的表示,Int()返回int64可处理所有有符号整型值(int8, int16, int32, int64)。

UInt()方法返回uint64,可处理每种有符号整型值(uint8, uint16, uint32, uint64)。

试图从不兼容类型的值(如字符串)获取整型值,将会引发恐慌。

为了避免恐慌,您可以先用Kind()方法检查值的类型。

获取值的所有方法:

  • Bool() bool

  • Int() int64

  • UInt() uint64

  • Float() float64

  • String() string

  • Bytes() []byte

设置值

setIntsetStructField展示的那样,只有拿到指向值的指针时,才可以修改值。

因为reflect.ValueOf()创建了一个reflect.Value 变量,它是一个指向值的指针变量,所以你需要用Elem()来获取reflect.Value所表示的值本身,然后再调用SetInt() 为它设置值。

setStructPtrField展示了如何通过字段在结构体中的位置来取得字段值的引用。

试图设置类型不兼容的值会导致恐慌。

设置值的方法跟读取值的方法是镜像存在的:

  • SetBool(v bool)

  • SetInt(v int64)

  • SetUInt(v uint64)

  • SetFloat(v float64)

  • SetString(v string)

  • SetBytes(v []byte)

最后更新于

这有帮助吗?