Go語言中使用反射的方法
更新時間:2015年02月20日 16:05:24 作者:不吃皮蛋
這篇文章主要介紹了Go語言中使用反射的方法,實例分析了Go語言實現(xiàn)反射的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Go語言中使用反射的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
// 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)建實例如下:
復(fù)制代碼 代碼如下:
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"]
完整代碼如下:
復(fù)制代碼 代碼如下:
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語言程序設(shè)計有所幫助。
相關(guān)文章
golang實現(xiàn)并發(fā)數(shù)控制的方法
下面小編就為大家分享一篇golang實現(xiàn)并發(fā)數(shù)控制的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12golang如何實現(xiàn)mapreduce單進程版本詳解
這篇文章主要給大家介紹了關(guān)于golang如何實現(xiàn)mapreduce單進程版本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Go語言中int、float、string類型之間相互的轉(zhuǎn)換
golang是強類型語言,在應(yīng)用過程中類型轉(zhuǎn)換基本都會用到,下面這篇文章主要給大家介紹了關(guān)于Go語言中int、float、string類型相互之間的轉(zhuǎn)換,需要的朋友可以參考下2022-01-01Go語言實戰(zhàn)之實現(xiàn)一個簡單分布式系統(tǒng)
如今很多云原生系統(tǒng)、分布式系統(tǒng),例如?Kubernetes,都是用?Go?語言寫的,這是因為?Go?語言天然支持異步編程。本篇文章將介紹如何用?Go?語言編寫一個簡單的分布式系統(tǒng),需要的小伙伴開業(yè)跟隨小編一起學(xué)習(xí)一下2022-10-10golang 在windows中設(shè)置環(huán)境變量的操作
這篇文章主要介紹了golang 在windows中設(shè)置環(huán)境變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04Golang實現(xiàn)Redis網(wǎng)絡(luò)協(xié)議實例探究
這篇文章主要為大家介紹了Golang實現(xiàn)Redis網(wǎng)絡(luò)協(xié)議實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01