我是 Golang 的新手,我試圖從結構中獲取許多屬性例如:
type Client struct{
name string//1
lastName string//2
age uint//3
}
func main(){
client := Client{name:"Facundo",lastName:"Veronelli",age:23}
fmt.println(client.getLengthAttibutes())//It would print "3"
}
使用反射包的ValueOf()
函數返回一個value
結構體。這有一個名為NumFields的方法,它返回字段的數量。
import (
"fmt"
"reflect"
)
type Client struct{
name string//1
lastName string//2
age uint//3
}
func main(){
client := Client{name:"Facundo",lastName:"Veronelli",age:23}
v := reflect.ValueOf(client)
fmt.Printf("Struct has %d fields", v.NumField())
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句