私はシステム アプリケーションの 1 つ、特に構成ファイル処理ビットに取り組んでいます。現在、構成ファイルを保存できる 3 つの異なる場所があり、後で拡張できる可能性があります。私がやろうとしているのは、新しいマネージド フィールドを追加するために必要な方法を単純化することです。
これまでの解決策は次のようになります。
package main
import (
    "reflect"
    "strconv"
    "strings"
)
type Datastore interface{}
type MyInt struct {
    intVal int
}
func NewMyInt(key string, dv int, db *Datastore) *MyInt {
    // Do something here to construct MyInt
    return &MyInt{intVal: dv}
}
type Config struct {
    myInts map[string]*MyInt
    // Tag is of form "<key in DB>:<default value>"
    Value1 MyInt "value1_key:12345"
    Value2 MyInt "value2_key:54321"
}
func NewConfig(db *Datastore) *Config {
    c := &Config{
        myInts: make(map[string]*MyInt),
    }
    cType := reflect.TypeOf(c)
    for i := 0; i < cType.NumField(); i++ {
        f := cType.Field(i)
        if f.Name == "myInts" {
            continue
        }
        tag := string(f.Tag)
        fields := strings.Split(tag, ":")
        switch f.Type.Name() {
        case "myInt":
            intVal, _ := strconv.Atoi(fields[1])
            val := NewMyInt(fields[0], intVal, db)
            c.myInts[fields[0]] = val
            // How do I set the i'th field to this newly constructed value?
        }
    }
    return c
}
これまでのところ、割り当てを行うためにこのピースが欠けています。