Home GoLang 结构体标签
Post
Cancel

GoLang 结构体标签

Golang Series

Hello GoLang: https://blog.yexca.net/archives/154
GoLang (var and const) 变量与常量: https://blog.yexca.net/archives/155
GoLang (func) 函数: https://blog.yexca.net/archives/156
GoLang (slice and map) 切片: https://blog.yexca.net/archives/160
GoLang (OOP) 面向对象: https://blog.yexca.net/archives/162
GoLang (reflect) 反射: https://blog.yexca.net/archives/204
GoLang (struct tag) 结构体标签: This Page
GoLang (goroutine) go 程: https://blog.yexca.net/archives/206
GoLang (channel) 通道: https://blog.yexca.net/archives/207


通过结构体标签可以描述该类在某包的作用

获取标签值

通过 ` 来定义 tag (markdown 代码块的键)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
	"fmt"
	"reflect"
)

type User struct {
    // 多个tag用空格隔离
	name string `doc:"name" info:"nameOfUser"`
	age  int    `info:"ageOfUser"`
}

func main() {
	user := User{"zhangSan", 18}

	findTag(&user)
}

func findTag(input interface{}) {
	t := reflect.TypeOf(input).Elem()

	for i := 0; i < t.NumField(); i++ {
		tagInfo := t.Field(i).Tag.Get("info")
		tagDoc := t.Field(i).Tag.Get("doc")
		fmt.Println("info:", tagInfo, "doc:", tagDoc)
	}
}

JSON 转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
    // 注意要是公有属性才可转换json
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	user := User{"zhangSan", 18}

	// struct --> json
	jsonStr, err := json.Marshal(user)
	if err != nil {
		fmt.Println("error", err)
	} else {
		fmt.Printf("jsonStr : %s\n", jsonStr)
	}

	// json --> struct
	var user2 User
	err = json.Unmarshal(jsonStr, &user2)
	if err != nil {
		fmt.Println("error", err)
	} else {
		fmt.Println(user2)
	}
}
This post is licensed under CC BY 4.0 by the author.

GoLang 反射

GoLang go 程