萬能程序
===
~~~
// reflect.Ptr 指針
// reflect.Struct 結構體
func fillBySettings(st interface{},settings map[string]interface{}) error {
if reflect.TypeOf(st).Kind() == reflect.Ptr { // 如果不是指針
fmt.Println("ptr")
// Elem() 獲取指針指向的值
return errors.New("st not ptr")
}else if reflect.TypeOf(st).Elem().Kind() != reflect.Struct{ // 如果不是結構
return errors.New("st not struct")
}
if settings == nil {
return errors.New("setting is nil")
}
var (
field reflect.StructField
ok bool
)
for k,v := range settings {
if field, ok = reflect.TypeOf(st).Elem().FieldByName(k);!ok {
continue
}
if field.Type == reflect.TypeOf(v) {
reflect.ValueOf(st).Elem().FieldByName(k).Set(reflect.ValueOf(v))
}
}
return nil
}
type hero struct {
Name string `hr:"ho"`
}
// 基礎測試
func TestBBst(t *testing.T) {
h := &hero{}
i := map[string]interface{}{
"Name": "dollarkiller",
}
bs := fillBySettings(h, i)
if bs != nil {
t.Log(bs.Error())
}else{
fmt.Println(i)
}
}
~~~
- Hello World
- UDP
- UDP服務端
- UDP客戶端
- UDP廣播
- 錯誤處理
- 編寫好的異常處理
- panic和recover
- 并發編程
- Hello Goruntine
- 共享內存并發機制
- RWMutex
- CSP并發機制
- 多路復用和超時控制
- 通道關閉與廣播
- Context與任務的取消
- 只運行一次
- 按需任意任務完成
- 所有任務完成
- 補充:range channel注意實現
- 對象池
- sync.Pool臨時對象池
- 單元測試
- 表格測試法
- Banchmark
- BDD
- 反射
- 利用反射編寫靈活的代碼
- Struct Tag
- 萬能程序
- 常用架構模式
- Pipe-filter pattern
- Micro Kernel
- 性能分析
- 高性能代碼
- sync.MAP分析
- Concurrent Map
- GC友好的代碼
- Uber開發風格規范