This is a creation in
Article, where the information may have evolved or changed.
The main function assigns values based on the value of the first constructor variable field that is not NULL with the second variable corresponding to the field
Remember. The slice type in the Golang is not to be compared.
Package Mainimport ("FMT" "reflect") type conf struct {S Stringi []int}func Main () {c1: = Conf{s: "Xiaochuan"}C2: = conf{i: []int{1, 2, 3}}v1: = reflect. ValueOf (C1)//Initialize V1V2: = Reflect for C1 safekeeping of the specific value. ValueOf (C2)//Initialize V2v1_elem: = Reflect for C2 safekeeping of the specific value. ValueOf (&C1). Elem ()//Returns the value held by the C1 pointer for i: = 0; I < v1. Numfield (); i++ {field: = v1. field (i)//return the first field of the struct Field2: = v2. field (i)//returns the first field of the struct//field. Interface () is currently holding a value of//reflect. Zero gets the corresponding 0 value based on the type//This must call the Interface method otherwise for reflect. The comparison of value constructs rather than the comparison of two values//this place should not be compared with the equals sign because the Golang slice type is an if reflect that does not support contrast. Deepequal (field. Interface (), reflect. Zero (field. Type ()). Interface ()) {//If a field of the first constructor corresponds to the default value of type if!reflect. Deepequal (Field2. Interface (), reflect. Zero (Field2. Type ()). Interface ()) {//if the second constructor of this field is not empty if V1_elem. Field (i). Canset ()! = true {//If the value cannot be set directly back to FMT. Println ("Not set Value") Return}v1_elem. Field (i). Set (Field2)//Setting value}}}FMT. PRINTLN (c1)//Output The value of the final C1}