使用反射設定巢狀結構體欄位
問題:
我們如何使用反射以持久的方式修改嵌套結構體欄位的值?
題目:
<code class="go">type ProductionInfo struct { StructA []Entry } type Entry struct { Field1 string Field2 int } func SetField(source interface{}, fieldName string, fieldValue string) { v := reflect.ValueOf(source) tt := reflect.TypeOf(source) for k := 0; k < tt.NumField(); k++ { fieldValue := reflect.ValueOf(v.Field(k)) fmt.Println(fieldValue.CanSet()) if fieldValue.CanSet() { fieldValue.SetString(fieldValue.String()) } } } func main() { source := ProductionInfo{} source.StructA = append(source.StructA, Entry{Field1: "A", Field2: 2}) SetField(source, "Field1", "NEW_VALUE") }</code>
問題:
反射無法修改非指標結構的欄位。我們需要傳遞一個指向 Entry 結構體的指標。
<code class="go">SetField(source.StructA[0], "Field1", "NEW_VALUE")</code>
<code class="go">func SetField(source *Entry, fieldName string, fieldValue string) { v := reflect.ValueOf(source).Elem() fmt.Println(v.FieldByName(fieldName).CanSet()) if v.FieldByName(fieldName).CanSet() { v.FieldByName(fieldName).SetString(fieldValue) } }</code>
<code class="go">package main import ( "fmt" "reflect" ) type ProductionInfo struct { StructA []Entry } type Entry struct { Field1 string Field2 int } func SetField(source *Entry, fieldName string, fieldValue string) { v := reflect.ValueOf(source).Elem() if v.FieldByName(fieldName).CanSet() { v.FieldByName(fieldName).SetString(fieldValue) } } func main() { source := ProductionInfo{} source.StructA = append(source.StructA, Entry{Field1: "A", Field2: 2}) fmt.Println("Before: ", source.StructA[0]) SetField(&source.StructA[0], "Field1", "NEW_VALUE") fmt.Println("After: ", source.StructA[0]) }</code>最終代碼:
Before: {A 2} true After: {NEW_VALUE 2}輸出:
以上是如何使用反射修改嵌套結構體欄位來實現持久化?的詳細內容。更多資訊請關注PHP中文網其他相關文章!