• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > Go语言中使用反射的方法

Go语言中使用反射的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了go语言方法,go语言,go语言环境搭建,go语言程序设计,let it go 25种语言等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Go语言中使用反射的方法。分享给大家供大家参考。具体实现方法如下:
type Dish struct {
  Id  int
  Name string
  Origin string
  Query func()
}</div>

创建实例如下:
shabushabu.instance_variables # => []
shabushabu.name = "Shabu-Shabu"
shabushabu.instance_variables # => ["@name"]
shabushabu.origin = "Japan"
shabushabu.instance_variables # => ["@name", "@origin"]</div>

完整代码如下:
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
}</div>

希望本文所述对大家的Go语言程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Go语言编程中字符串切割方法小结
  • Go语言中字符串的查找方法小结
  • go语言获取系统盘符的方法
  • go语言使用scp的方法实例分析
  • go语言使用RC4加密的方法
  • Go语言单链表实现方法
  • go语言中时间戳格式化的方法
  • go语言操作redis连接池的方法
  • go语言检测文件是否存在的方法
  • Go语言生成素数的方法

相关文章

  • 深入理解GO语言的面向对象
  • Go语言函数学习教程
  • GO语言运行环境下载、安装、配置图文教程
  • Go语言开发环境搭建与初探(Windows平台下)
  • 在Visual Studio Code中配置GO开发环境的详细教程
  • 利用Go语言实现简单Ping过程的方法
  • golang基于websocket实现的简易聊天室程序
  • Golang 内存模型详解(一)
  • Go语言按字节截取字符串的方法
  • Golang排列组合算法问题之全排列实现方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Go语言对JSON进行编码和解码的方法
    • Golang记录、计算函数执行耗时、运行时间的一个简单方法
    • 详解Golang编程中的常量与变量
    • Go语言获取本机逻辑CPU数量的方法
    • go语言实现通过FTP库自动上传web日志
    • 浅谈Go语言中字符串和数组
    • Go语言常用字符串处理方法实例汇总
    • golang简单读写文件示例
    • go语言实现sqrt的方法
    • go语言睡眠排序算法实例分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有