惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Troy Hunt's Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
T
Tenable Blog
L
LINUX DO - 热门话题
V
Visual Studio Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
SecWiki News
SecWiki News
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
S
Securelist
Spread Privacy
Spread Privacy
L
LangChain Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
博客园 - 司徒正美
Help Net Security
Help Net Security
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
博客园 - 【当耐特】
T
Tor Project blog

ZDDHUB

PixelsMeasure 开发第二年总结 PixelsMeasure 开发一年总结 Swift/SwiftUI 踩坑记 为什么说 GPT 利好程序员 ChatGPT 编程实现 Web 数字水印 Web 数字水印探究 Micro Frontends for Mobile URL 加载系统(URL Loading System) Protocol Buffers GraphQL 从 0 到 1 开发一款 IOS 应用 - Swift MV* 软件设计架构 学习一个新技巧需要多久? 不停机数据库迁移 Rspec 如何 mock update 方法更新自己? Rails 使用 mysql2 出现的段错误 使用 Docker-compose 部署 Rails 应用到生产环境 Cocoa troubleshooting 独孤九剑 Dit (0x05) - 终端篇 Gem-based Jekyll theme 开发小记 Miscellaneous 前端手记 TodoMVC 之 Redux 篇 前端手记 TodoMVC 之 Server 篇 前端手记 TodoMVC 之 React 篇 前端手记 TodoMVC 之 CSS 篇 独孤九剑 Dit (0x04) - 测试篇 独孤九剑 Dit (0x03) - 缓存篇 英语小抄 LLDB debug Golang Make mistakes 大牛俱乐部上线啦 独孤九剑 Dit (0x02) - 数据结构篇 独孤九剑 Dit (0x01) - 总决 独孤九剑 Dit (0x00) - 我为什么要做 Dit 零值强制类型转换的使用 终端颜色输出重定向 Go语法简略 - 正则表达式 Makefile Go语法简略 - Duck框架探索 Go语法简略 - 依赖注入 Go语法简略 - web应用框架 Go语法简略 - 面向对象 Go语法简略 - goroutine Go语法简略 - 方法和接口 Go语法简略 - 基础篇 大牛 轻松科研 未来这几年 为 Android Studio 创建图标 Shell Git Vim 金庸答百问 论拖延症 Flex, A fast scanner generator 有理想的人 从虚拟到现实 常用视频转接口 Recognizer configuration on CentOS 整个世界清静了 《Python源码剖析》读书笔记
Go语法简略 - 反射
zddhub · 2015-07-03 · via ZDDHUB
package main

import (
  "fmt"
  "reflect"
)

// reflect的用法, 包括访问、设置属性和方法调用。

func Info(i interface{}) {
  t := reflect.TypeOf(i)
  v := reflect.ValueOf(i)
  fmt.Println(t, v)

  // 检测不为struct类型时退出
  if k := t.Kind(); k != reflect.Struct {
    return
  }
  // Type:
  fmt.Println(t.PkgPath(), t.Name(), "")
  fmt.Println("- Field num:", t.NumField(), " Method num:", t.NumMethod())

  // 输出属性
  for j := 0; j < t.NumField(); j++ {
    f := t.Field(j)
    fmt.Println("-", f.Name, f.Type)
  }
  // 输出方法
  for j := 0; j < t.NumMethod(); j++ {
    m := t.Method(j)
    fmt.Println("+", m.Name, m.Type)
  }
}

// 获取struct属性的值
func GetValue(o interface{}) {
  v := reflect.ValueOf(o)
  fmt.Println(o)
  // 检测不为struct类型时退出
  if k := v.Kind(); k != reflect.Struct {
    return
  }
  for i := 0; i < v.NumField(); i++ {
    f := v.Field(i)
    if f.CanInterface() {
      // 读取未导出变量时会panic:
      // panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
      // 用CanInterface过滤
      if k := f.Kind(); k == reflect.Struct {
        sf := f.Field(i)
        fmt.Println("-", sf.Interface(), sf.Type())
      } else {
        fmt.Println("-", f.Interface(), f.Type())
      }
    }
  }
}

// 设置属性的值
func SetValue(o interface{}, name string, value interface{}) {
  v := reflect.ValueOf(o)
  if k := v.Kind(); k != reflect.Ptr || !v.Elem().CanSet() {
    fmt.Println("Invalid set type:", o)
    return
  }
  v = v.Elem()
  f := v.FieldByName(name)
  if !f.IsValid() {
    fmt.Println("Invalid name:", name)
  }

  f.Set(reflect.ValueOf(value))
}

// 调用方法
func CallMethod(o interface{}, name string, args interface{}) {
  v := reflect.ValueOf(o)
  mn := v.MethodByName(name)
  fmt.Println(mn, mn.Kind())
  if mn.Kind() == reflect.Func {
    if mn.Type().NumIn() == 0 {
      mn.Call([]reflect.Value{})
    } else {
      mn.Call([]reflect.Value{reflect.ValueOf(args)})
    }
  }
}

type Human struct {
  Name string
  Age  int
  url  string
}

func (h Human) SayHello() {
  fmt.Println("Hi, I am ", h.Name)
}

func (h Human) SetAge(age int) {
  h.Age = age
  fmt.Println("I'm in Set Age:", h.Age)
}

func (h *Human) SetUrl(url string) {
  h.url = url
}

type Boss struct {
  Human
  money int
}

func main() {
  // 获取属性信息和方法
  Info(1)
  Info(11.15)
  Info("String-Value")
  //: int <int Value>
  //: float64 <float64 Value>
  //: string String-Value

  h := Human{"zddhub", 27, "www.zddhub.com"}
  Info(h)
  //: main.Human <main.Human Value>
  //: main Human
  //: - Field num: 3  Method num: 2
  //: - Name string
  //: - Age int
  //: - url string
  //: + SayHello func(main.Human)
  //: + SetAge func(main.Human, int)
  //: 接收者为指针的函数没有被输出

  b := Boss{Human{"Jobs", 30, "www.daniu.io"}, 1e10}
  Info(b)
  //: main.Boss <main.Boss Value>
  //: main Boss
  //: - Field num: 2  Method num: 2
  //: - Human main.Human
  //: - money int
  //: + SayHello func(main.Boss)
  //: + SetAge func(main.Boss, int)
  //: 接受者为指针的函数没有被输出

  // 返回属性的值
  GetValue(h)
  //: {zddhub 27 www.zddhub.com}
  //: - zddhub string
  //: - 27 int
  GetValue(b)
  //: { {Jobs 30 www.daniu.io} 10000000000}
  //: 双括号会和jekyll语法冲突,所以上一行中间加了一个空格
  //: - Jobs string

  // 设置属性的值
  fmt.Println(h.Age, h.Name)
  //: 27 zddhub
  SetValue(h, "Age", 10)
  //: Invalid set type: {zddhub 27 www.zddhub.com}
  SetValue(h, "Name", "wx")
  //: Invalid set type: {zddhub 27 www.zddhub.com}
  fmt.Println(h.Age, h.Name)
  //: 27 zddhub
  SetValue(&h, "Age", 200)
  SetValue(&h, "Name", "wx")
  fmt.Println(h.Age, h.Name)
  //: 200 wx

  // 方法调用
  CallMethod(h, "SayHello", nil)
  //: <func() Value> func
  //: Hi, I am  wx
  CallMethod(&h, "SetAge", 80) // 函数的接受者为copy的另一对象,设置不成功
  fmt.Println(h.Age, h.Name, h.url)
  //: <func(int) Value> func
  //: I'm in Set Age: 80
  //: 200 wx www.zddhub.com

  CallMethod(&h, "SetUrl", "www.daniu.io") // 接收者为指针类型,设置成功
  fmt.Println(h.url)
  //: <func(string) Value> func
  //: www.daniu.io
}