Summary of go language series basic tutorials (3)

1. Universal data type 

interface{ } can represent any data type

2. Visibility of methods in packages

If the method in the package needs to be referenced externally, the first character of the method needs to be capitalized.

3. Reflection in go language

reflect package func ValueOf(i interface{}) Value {...} get parameter value

func TypeOf(i interface{}) Type{...} Get the type of the parameter

t := reflect.TypeOf(stu1)

t.NumField(); Get the number of all fields

field := t.Field(i) gets a field

t.NumMethod() gets the number of all methods
    
 method := v.Method(i) gets a method

4. JSON processing in go language

  // Internal map
    m := make(map[string] interface{}, 0)
    if err := json.Unmarshal([]byte(jsonStr), &m); err != nil {         panic(err)     }


 

    // deserialize into object
    re := Requst{}
    ​​if err := json.Unmarshal([]byte(jsonStr), &re); err != nil {         panic(err)     }


 

    // Get a single key,
    userName := gjson.Get(jsonStr, "userName")


 

Guess you like

Origin blog.csdn.net/u013558123/article/details/131396346