diff --git a/Golang/Golang Document.md b/Golang/Golang Document.md index 10ed0a7..ffda9d7 100644 --- a/Golang/Golang Document.md +++ b/Golang/Golang Document.md @@ -1,43 +1,50 @@ - [Golang](#golang) - - [Get Started](#get-started) - - [Enable dependency tracking](#enable-dependency-tracking) - - [go mod init](#go-mod-init) - - [go mod tidy](#go-mod-tidy) - - [multi-module workspace](#multi-module-workspace) - - [go work init](#go-work-init) - - [go work use](#go-work-use) - - [Gin框架构建restful api](#gin框架构建restful-api) - - [向response中写入返回数据](#向response中写入返回数据) - - [解析request中的数据](#解析request中的数据) - - [将请求的endpoint注册到server中](#将请求的endpoint注册到server中) - - [golang generic](#golang-generic) - - [不使用泛型的代码编写](#不使用泛型的代码编写) - - [使用泛型的代码编写](#使用泛型的代码编写) - - [comparable](#comparable) - - [泛型方法调用](#泛型方法调用) - - [type constraint](#type-constraint) - - [Fuzzing](#fuzzing) - - [unit test编写](#unit-test编写) - - [fuzz test编写](#fuzz-test编写) - - [执行fuzz test](#执行fuzz-test) - - [sync.Pool](#syncpool) - - [sync.Pool使用示例](#syncpool使用示例) - - [Pool和垃圾回收](#pool和垃圾回收) - - [poolCleanup](#poolcleanup) - - [allPools \& oldPools](#allpools--oldpools) - - [Proc Pining](#proc-pining) - - [per-P](#per-p) - - [local \& localSize](#local--localsize) - - [PinSlow](#pinslow) - - [Pool Local](#pool-local) - - [pool的Put/Get](#pool的putget) - - [Put](#put) - - [Get](#get) - - [slow path](#slow-path) - - [Sync.once](#synconce) - - [use case](#use-case) - - [syntax](#syntax) - - [iota](#iota) + - [Get Started](#get-started) + - [Enable dependency tracking](#enable-dependency-tracking) + - [go mod init](#go-mod-init) + - [go mod tidy](#go-mod-tidy) + - [multi-module workspace](#multi-module-workspace) + - [go work init](#go-work-init) + - [go work use](#go-work-use) + - [Gin框架构建restful api](#gin框架构建restful-api) + - [向response中写入返回数据](#向response中写入返回数据) + - [解析request中的数据](#解析request中的数据) + - [将请求的endpoint注册到server中](#将请求的endpoint注册到server中) + - [golang generic](#golang-generic) + - [不使用泛型的代码编写](#不使用泛型的代码编写) + - [使用泛型的代码编写](#使用泛型的代码编写) + - [comparable](#comparable) + - [泛型方法调用](#泛型方法调用) + - [type constraint](#type-constraint) + - [Fuzzing](#fuzzing) + - [unit test编写](#unit-test编写) + - [fuzz test编写](#fuzz-test编写) + - [执行fuzz test](#执行fuzz-test) + - [sync.Pool](#syncpool) + - [sync.Pool使用示例](#syncpool使用示例) + - [Pool和垃圾回收](#pool和垃圾回收) + - [poolCleanup](#poolcleanup) + - [allPools \& oldPools](#allpools--oldpools) + - [Proc Pining](#proc-pining) + - [per-P](#per-p) + - [local \& localSize](#local--localsize) + - [PinSlow](#pinslow) + - [Pool Local](#pool-local) + - [pool的Put/Get](#pool的putget) + - [Put](#put) + - [Get](#get) + - [slow path](#slow-path) + - [Sync.once](#synconce) + - [use case](#use-case) + - [reflect](#reflect) + - [reflect basic](#reflect-basic) + - [Type](#type) + - [Kind](#kind) + - [常用方法](#常用方法) + - [NumField](#numfield) + - [Field](#field) + - [syntax](#syntax) + - [iota](#iota) @@ -805,6 +812,66 @@ func (o *Once) doSlow(f func()) { } ``` +## reflect +### reflect basic +go reflection基于`Values, Types, Kinds`,其相关类为`reflect.Value, reflect.Type, reflect.Kind`,获取方式如下: +- `reflect.ValueOf(x interface{})` +- `refelct.TypeOf(x interface{})` +- `Type.kind()` + +#### Type +type用于表示go中的类型,通过`reflect.TypeOf(x interface{})`来进行获取 +```go +fmt.Println(reflect.TypeOf(Addr{})) // main.Addr +fmt.Println(reflect.TypeOf(&Addr{})) // *main.Addr +``` + +#### Kind +kind用于表示`类型`的数据类型,通过`.Kind`来进行获取: +```go +fmt.Println(reflect.TypeOf(Addr{}).Kind()) // struct +fmt.Println(reflect.TypeOf(&Addr{}).Kind()) // ptr +``` + +### 常用方法 +golang反射还提供了其他的一些常用方法。 + +#### NumField +该方法返回struct中的fields数量,如果类型参数其kind不为`reflect.struct`,那么其会发生`panic` + +```go +fmt.Println(reflect.TypeOf(Addr{}).NumField()) // 2 +``` + +#### Field +该方法允许通过下标来访问struct中的field。 + +```go +t := reflect.TypeOf(Addr{}) +for i := 0; i < t.NumField(); i++ { + fmt.Println(t.Field(i)) +} +``` +上述输出为 +``` +{FrmIp string 0 [0] false} +{DstIp string 16 [1] false} +``` + +根据反射遍历struct中的field,其示例如下: +```go +var v interface{} = Addr{FrmIp: "1.2.3.4", DstIp: "7.8.9.10"} + m := reflect.TypeOf(v).NumField() + for i := 0; i < m; i++ { + fn := reflect.TypeOf(v).Field(i).Name + fv := reflect.ValueOf(v).Field(i).String() + fmt.Println(fn, fv) + } +``` + + + + ## syntax ### iota `iota`关键字代表连续的整数变量,`0, 1, 2`,每当`const`关键字出现时,其重置为0