Go the struct

1 Introduction

Go struct declaration allows the field that came Tagto do some mark on the field.

This Tagis not just a simple string, because it is mainly used for reflection scenario, reflectthe packet is provided an operation Tagmethod, the Tagwording must follow certain rules.

2. Tag nature of

2.1 Tag Rules

TagItself is a string, but the string is: 以空格分隔的 key:value 对.

  • key: Be non-empty string, the string can not contain control characters, space, quotation mark, colon.
  • value: String double quotation marks
  • Note: You can not have spaces before and after the colon

As shown in the following code, so writing has no real meaning, for illustration only Tagrule

type Server struct {
    ServerName string `key1: "value1" key11:"value11"`
    ServerIP   string `key2: "value2"`
}

The code ServerNamefield Tagcontains two key-value pair. ServerIPField Tagcontains only a key-value pair.

2.2 Tag is part of Struct

As mentioned above, Taguseful only in the reflective scene, reflected package provides an operating Tagmethod. Before said method, it is necessary to look at how to manage Go struct field.

The following is a reflecttype declaration package, part of the field is omitted herein independent.

// A StructField describes a single field in a struct.
type StructField struct {
    // Name is the field name.
    Name string
    ...
    Type      Type      // field type
    Tag       StructTag // field tag string
    ...
}

type StructTag string

Visible, a description of the structure member contains structure StructTag, which itself is a string. That Tagis actually an integral part of the structure of the field.

2.3 Gets Tag

StructTagIt provides a Get(key string) stringmethod to obtain Tag, for example:

package main

import (
    "reflect"
    "fmt"
)

type Server struct {
    ServerName string `key1:"value1" key11:"value11"`
    ServerIP   string `key2:"value2"`
}

func main() {
    s := Server{}
    st := reflect.TypeOf(s)

    field1 := st.Field(0)
    fmt.Printf("key1:%v\n", field1.Tag.Get("key1"))
    fmt.Printf("key11:%v\n", field1.Tag.Get("key11"))

    filed2 := st.Field(1)
    fmt.Printf("key2:%v\n", filed2.Tag.Get("key2"))
}

Program output is as follows:

key1:value1
key11:value11
key2:value2

3. Tag the meaning of existence

Tag examples in this article do not have any real meaning, which is to elaborate tag definitions and methods of operation, and to avoid before you ever seen such json:xxxconfusion.

Using reflection can be dynamically assigned to the structure members, it is because of tag, you can use the tag to determine the action assignment before the assignment.
For example, the official encoding/jsonpacket may be a JSON data Unmarshalinto a structure, the use of this process to the Tag. This package defines rules, as long as the tag is provided with reference to the rule can be converted into a different data structure of JSON.

In short: It is based on the characteristics of a struct tag, only such json, orm, etc. applications. Understanding this relationship is critical. Perhaps, you can define another tag rules, to handle your specific data.

4. Tag common usage

Common tag usage, mainly JSON data analysis, ORM mapping. ====

Guess you like

Origin www.cnblogs.com/wangjiale1024/p/11719897.html