好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Go語言中使用反射的方法

Go語言中使用反射的方法

熱門標簽:海外照相館地圖標注入駐 外呼系統多少錢一年 工商信用卡外呼系統教程 客服級電銷機器人 經常接到推銷電話機器人的電話 滁州自建外呼系統 旅游廁所如何電子地圖標注 外呼系統如何接收服務密碼 智能營銷軟件

本文實例講述了Go語言中使用反射的方法。分享給大家供大家參考。具體實現方法如下:

復制代碼 代碼如下:
// Data Model
type Dish struct {
  Id  int
  Name string
  Origin string
  Query func()
}

創建實例如下:

復制代碼 代碼如下:
shabushabu = Dish.new
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
}

希望本文所述對大家的Go語言程序設計有所幫助。

您可能感興趣的文章:
  • golang之反射和斷言的具體使用
  • 詳解Golang利用反射reflect動態調用方法
  • 淺談Go語言中的結構體struct & 接口Interface & 反射
  • Go語言學習筆記之反射用法詳解
  • Go語言中反射的正確使用
  • 談談Go語言的反射三定律
  • go語言通過反射獲取和設置結構體字段值的方法
  • 圖文詳解go語言反射實現原理

標簽:喀什 九江 湘潭 運城 楚雄 晉城 深圳 本溪

巨人網絡通訊聲明:本文標題《Go語言中使用反射的方法》,本文關鍵詞  語,言中,使用,反射,的,方法,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Go語言中使用反射的方法》相關的同類信息!
  • 本頁收集關于Go語言中使用反射的方法的相關信息資訊供網民參考!
  • 推薦文章