亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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()
}

創(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"]

完整代碼如下:

復(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
}

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

相關(guān)文章

  • golang實現(xiàn)并發(fā)數(shù)控制的方法

    golang實現(xiàn)并發(fā)數(shù)控制的方法

    下面小編就為大家分享一篇golang實現(xiàn)并發(fā)數(shù)控制的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • golang如何實現(xiàn)mapreduce單進程版本詳解

    golang如何實現(xiàn)mapreduce單進程版本詳解

    這篇文章主要給大家介紹了關(guān)于golang如何實現(xiàn)mapreduce單進程版本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Go語言中int、float、string類型之間相互的轉(zhuǎn)換

    Go語言中int、float、string類型之間相互的轉(zhuǎn)換

    golang是強類型語言,在應(yīng)用過程中類型轉(zhuǎn)換基本都會用到,下面這篇文章主要給大家介紹了關(guān)于Go語言中int、float、string類型相互之間的轉(zhuǎn)換,需要的朋友可以參考下
    2022-01-01
  • golang關(guān)閉chan通道的方法示例

    golang關(guān)閉chan通道的方法示例

    在go語言中,通道(channel)是一個非常重要的概念,通道提供了一種在不同 goroutine 之間安全地傳遞數(shù)據(jù)的方式,在本文中,我們將討論如何關(guān)閉通道以及在關(guān)閉通道時需要考慮的事項,需要的朋友可以參考下
    2024-02-02
  • Go語言實戰(zhàn)之實現(xiàn)一個簡單分布式系統(tǒng)

    Go語言實戰(zhàn)之實現(xiàn)一個簡單分布式系統(tǒng)

    如今很多云原生系統(tǒng)、分布式系統(tǒng),例如?Kubernetes,都是用?Go?語言寫的,這是因為?Go?語言天然支持異步編程。本篇文章將介紹如何用?Go?語言編寫一個簡單的分布式系統(tǒng),需要的小伙伴開業(yè)跟隨小編一起學(xué)習(xí)一下
    2022-10-10
  • golang 在windows中設(shè)置環(huán)境變量的操作

    golang 在windows中設(shè)置環(huán)境變量的操作

    這篇文章主要介紹了golang 在windows中設(shè)置環(huán)境變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Golang實現(xiàn)Redis網(wǎng)絡(luò)協(xié)議實例探究

    Golang實現(xiàn)Redis網(wǎng)絡(luò)協(xié)議實例探究

    這篇文章主要為大家介紹了Golang實現(xiàn)Redis網(wǎng)絡(luò)協(xié)議實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Go語言中獲取IP地址的方法詳解

    Go語言中獲取IP地址的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Go語言中獲取IP地址的相關(guān)方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • golang判斷兩個事件是否存在沖突的方法示例

    golang判斷兩個事件是否存在沖突的方法示例

    這篇文章主要為大家詳細(xì)介紹了golang判斷兩個事件是否存在沖突的方法示例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • Golang中Bit數(shù)組的實現(xiàn)方式

    Golang中Bit數(shù)組的實現(xiàn)方式

    這篇文章主要介紹了Golang中Bit數(shù)組的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評論