Fold knowledge into data so program logic can be stupid and robust.
Data is more tractable than program logic. It follows that where you see a choice between complexity in data structures and complexity in code, choose the former. More: in evolving a design, you should actively seek ways to shift complexity from code to data.
中譯:
將知識塞進資料,好讓程式的邏輯變得單純卻強固。
資料比起程式邏輯更容易被理解。因此,當你發現可以選擇資料複雜性或是程式碼複雜性時,選擇前者。更進一步來說,在系統設計演進的過程,你應該積極地尋找方式,好讓複雜性從程式碼進入資料。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type contact struct { | |
Cell string `json:cell` | |
Email string `json:email` | |
RealName string `json:realname` | |
Phone string `json:telphone` | |
ID string `user_id` | |
} | |
type contactTuple struct { | |
Backuper []contact `json:"backuper"` | |
Principal []contact `json:"principal"` | |
Upgrader []contact `json:"upgrader"` | |
} | |
type contactInfo struct { | |
Status int `json:"status"` | |
Info string `json:"info"` | |
Count int `json:"count"` | |
Result map[string]contactTuple `json:"result"` | |
} | |
func postJSON(params map[string]string, url string, v interface{}) error { | |
bs, err := json.Marshal(params) | |
if err != nil { | |
return err | |
} | |
req, err := http.NewRequest("POST", url, bytes.NewReader(bs)) | |
if err != nil { | |
return err | |
} | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
return json.NewDecoder(resp.Body).Decode(v) | |
} | |
func getPlatformContactNew(platformName string, fcname string, fctoken string, url string) (contactInfo, error) { | |
params := map[string]string{ | |
"fcname": fcname, | |
"fctoken": fctoken, | |
"platform_key": platformName, | |
} | |
data := contactInfo{} | |
err := postJSON(params, url, &data) | |
return data, err | |
} |