Go語言中使用反射的方法
更新時間:2015年02月20日 16:05:24 作者:不吃皮蛋
這篇文章主要介紹了Go語言中使用反射的方法,實例分析了Go語言實現反射的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Go語言中使用反射的方法。分享給大家供大家參考。具體實現方法如下:
復制代碼 代碼如下:
// Data Model
type Dish struct {
Id int
Name string
Origin string
Query func()
}
type Dish struct {
Id int
Name string
Origin string
Query func()
}
創(chuàng)建實例如下:
復制代碼 代碼如下:
shabushabu = Dish.new
shabushabu.instance_variables # => []
shabushabu.name = "Shabu-Shabu"
shabushabu.instance_variables # => ["@name"]
shabushabu.origin = "Japan"
shabushabu.instance_variables # => ["@name", "@origin"]
shabushabu.instance_variables # => []
shabushabu.name = "Shabu-Shabu"
shabushabu.instance_variables # => ["@name"]
shabushabu.origin = "Japan"
shabushabu.instance_variables # => ["@name", "@origin"]
完整代碼如下:
復制代碼 代碼如下:
package main
import(
"fmt"
"reflect"
)
func main(){
// iterate through the attributes of a Data Model instance
for name, mtype := range attributes(&Dish{}) {
fmt.Printf("Name: %s, Type %s\n", name, mtype.Name())
}
}
// Data Model
type Dish struct {
Id int
Name string
Origin string
Query func()
}
// Example of how to use Go's reflection
// Print the attributes of a Data Model
func attributes(m interface{}) (map[string]reflect.Type) {
typ := reflect.TypeOf(m)
// if a pointer to a struct is passed, get the type of the dereferenced object
if typ.Kind() == reflect.Ptr{
typ = typ.Elem()
}
// create an attribute data structure as a map of types keyed by a string.
attrs := make(map[string]reflect.Type)
// Only structs are supported so return an empty result if the passed object
// isn't a struct
if typ.Kind() != reflect.Struct {
fmt.Printf("%v type can't have attributes inspected\n", typ.Kind())
return attrs
}
// loop through the struct's fields and set the map
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
attrs[p.Name] = p.Type
}
}
return attrs
}
import(
"fmt"
"reflect"
)
func main(){
// iterate through the attributes of a Data Model instance
for name, mtype := range attributes(&Dish{}) {
fmt.Printf("Name: %s, Type %s\n", name, mtype.Name())
}
}
// Data Model
type Dish struct {
Id int
Name string
Origin string
Query func()
}
// Example of how to use Go's reflection
// Print the attributes of a Data Model
func attributes(m interface{}) (map[string]reflect.Type) {
typ := reflect.TypeOf(m)
// if a pointer to a struct is passed, get the type of the dereferenced object
if typ.Kind() == reflect.Ptr{
typ = typ.Elem()
}
// create an attribute data structure as a map of types keyed by a string.
attrs := make(map[string]reflect.Type)
// Only structs are supported so return an empty result if the passed object
// isn't a struct
if typ.Kind() != reflect.Struct {
fmt.Printf("%v type can't have attributes inspected\n", typ.Kind())
return attrs
}
// loop through the struct's fields and set the map
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
attrs[p.Name] = p.Type
}
}
return attrs
}
希望本文所述對大家的Go語言程序設計有所幫助。
相關文章
Go語言中int、float、string類型之間相互的轉換
golang是強類型語言,在應用過程中類型轉換基本都會用到,下面這篇文章主要給大家介紹了關于Go語言中int、float、string類型相互之間的轉換,需要的朋友可以參考下2022-01-01Go語言實戰(zhàn)之實現一個簡單分布式系統(tǒng)
如今很多云原生系統(tǒng)、分布式系統(tǒng),例如?Kubernetes,都是用?Go?語言寫的,這是因為?Go?語言天然支持異步編程。本篇文章將介紹如何用?Go?語言編寫一個簡單的分布式系統(tǒng),需要的小伙伴開業(yè)跟隨小編一起學習一下2022-10-10golang 在windows中設置環(huán)境變量的操作
這篇文章主要介紹了golang 在windows中設置環(huán)境變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04