{"rsdb":{"rid":"301372","subhead":"","postdate":"0","aid":"216692","fid":"78","uid":"1","topic":"1","content":"
\n

\u6b63\u6587<\/h1> \n
\u8fd9\u91cc\u7ed9\u5927\u5bb6\u603b\u7ed3\u4e00\u4e9b Go player \u5f00\u53d1\u5c0f\u6280\u5de7. \u6b22\u8fce\u6279\u8bc4\u548c\u4ea4\u6d41, \u671b\u5927\u5bb6\u559c\u6b22. <\/code><\/pre> \n 

1. \u914d\u7f6e\u7ba1\u7406<\/h3> \n
\u63a8\u8350\u4e00\u79cd\u7b80\u5355\u7c97\u66b4\u7684\u914d\u7f6e\u7ba1\u7406\u65b9\u5f0f [\u914d\u7f6e \u6620\u5c04 \u5185\u90e8\u7ed3\u6784]. \n\u4f8b\u5982\u6709\u4e2a\u914d\u7f6e\u6587\u4ef6 config.online.yaml<\/code><\/pre> \n 
# \u5e38\u91cf\npi: 3.14159265358\n\n# \u5373\u8868\u793a\u7f51\u5740\u5c5e\u6027\u503c\nuri: https:\/\/www.google.com\n\n# \u5373\u8868\u793a server.host \u5c5e\u6027\u7684\u503c\nserver:\n    host: http:\/\/www.youtube.com\n\n# \u6570\u7ec4, \u5373\u8868\u793a server \u4e3a [a, b, c]\nhost:\n    - 172.217.161.132\n    - 216.58.220.206\n    - 8.8.8.8<\/code><\/pre> \n 
\u6211\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u76f4\u63a5\u5199\u6620\u5c04\u89c4\u5219.<\/code><\/pre> \n 
var C = struct {\n    PI float64 `yaml:"pi"`\n    URL `yaml:"uri"`\n    Server struct {\n        Host `yaml:"host"`\n    } `yaml:"server"`\n    Host []string `yaml:"host"`\n}{}<\/code><\/pre> \n 
\u7a0b\u5e8f\u542f\u52a8\u65f6\u5019, \u901a\u8fc7 func init() {} \u521d\u59cb\u5316. \u4f7f\u7528\u65f6\u53ea\u9700\u8981\u4f7f\u7528 config.C.PI, \n\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf. \u518d\u8865\u5145\u4e00\u4e2a\u66f4\u597d\u7684\u914d\u7f6e\u6587\u4ef6\u534f\u8bae toml.<\/code><\/pre> \n 

toml<\/a><\/p> \n

\u5982\u679c\u6362\u7528 toml \u914d\u7f6e(config.online.toml)\u7684\u5185\u5bb9\u66f4\u597d\u7406\u89e3<\/code><\/pre> \n 
pi  = 3.14159265358\nuri = https:\/\/www.google.com\n\n[server]\nhost = http:\/\/www.youtube.com\n\nhost = [\n    "172.217.161.132",\n    "216.58.220.206",\n    "8.8.8.8"\n]<\/code><\/pre> \n 
\u771f\u7684, \u770b\u89c1 toml \u7684\u7b2c\u4e00\u773c\u5c31\u559c\u6b22\u4e0a\u4e86. \u597d\u8212\u670d ~ \u8ba9\u4eba\u89c9\u5f97\u597d\u8212\u670d, \u5c31\u5e94\u8be5\u8fd9\u6837\u7684\u96d5\u7422.<\/code><\/pre> \n 

2. fmt.Sprintf<\/h3> \n
\u6709\u65f6\u5019\u6211\u4eec\u770b\u89c1\u8fd9\u6837\u7684\u4ee3\u7801\u7247\u6bb5<\/code><\/pre> \n 
    if len(v) > 0 {\n        errMessage = fmt.Sprintf(t, v...)\n    } else {\n        errMessage = t\n    }<\/code><\/pre> \n 
\u5176\u5b9e\u5bf9\u4e8e fmt.Sprintf \u662f\u753b\u86c7\u6dfb\u8db3, \u53ef\u4ee5\u76f4\u63a5<\/code><\/pre> \n 
    errMessage = fmt.Sprintf(t, v...)<\/code><\/pre> \n 

3. \u4e52\u4e53\u7ed3\u6784<\/h3> \n
(\u8bf4\u7684\u5f88\u8f7b\u5de7, \u63a8\u8350\u6709\u6240\u601d\u8003) \u666e\u901a\u7684\u8bfb\u5199\u64cd\u4f5c\u4ee3\u7801\u6709<\/code><\/pre> \n 
var lastMd5sLock   = sync.RWMutex{}\nvar lastMd5s map[string]map[string]string\n\nfunc ClearCache() {\n    lastMd5sLock.Lock()\n    defer lastMd5sLock.Unlock()\n    lastMd5s = make(map[string]map[string]string)\n}<\/code><\/pre> \n 
\u8fd9\u91cc\u5206\u4eab\u4e2a\u5e72\u6389 RWMutex \u7684\u65e0\u9501\u6280\u5de7. \u8fd0\u7528\u65b0\u65e7\u4e24\u4efd\u914d\u7f6e, \u4f7f\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\u6280\u5de7.<\/code><\/pre> \n 
var nowIndex uint32\nvar dataConf [2]map[string]map[string]string\n\n\/\/ ClearCache conf map clear\nfunc ClearCache() {\n    lastConf := make(map[string]map[string]string)\n    lastIndex := 1 - atomic.LoadUint32(&nowIndex)\n    dataConf[lastIndex] = lastConf\n    atomic.StoreUint32(&nowIndex, lastIndex)\n}<\/code><\/pre> \n 
\u6211\u4eec\u6765\u8bb2\u89e3\u4ee3\u7801, \u539f\u5148\u7684 ClearCache \u90a3\u6bb5\u4ee3\u7801\u52a0\u4e86\u5199\u9501. \u5199\u9501\u80fd\u591f\u505a\u5230\u4e24\u4ef6\u4e8b\u60c5\n1' \u4e34\u754c\u60c5\u51b5\u6709\u4eba\u5728\u5355\u6761\u8bfb\u53d6, \u6e05\u9664\u4f1a\u8ba9\u5176\u7b49\u5f85\n2' \u4e34\u754c\u60c5\u51b5\u6709\u4eba\u5728\u5355\u6761\u5199\u5165, \u6e05\u9664\u4f1a\u8ba9\u5176\u7b49\u5f85\n\n\u5047\u5982\u6211\u4eec\u4e0d\u5bf9 ClearCache \u52a0\u5199\u9501, \u91c7\u7528\u539f\u5b50\u4ea4\u6362\u6280\u5de7.\n\n\u7531\u4e8e\u6b64\u523b\u5185\u5b58\u4e2d\u5b58\u5728 dataConf[1] new \u548c dataConf[0] old \u4e24\u4e2a\u914d\u7f6e\u5bf9\u8c61.\n\u4e34\u754c\u60c5\u51b5\u6307\u8bfb\u53d6\u548c\u5199\u5165\u90fd\u5728\u8fdb\u884c, \u4f46\u6b64\u523b\u89e6\u53d1\u6e05\u9664\u64cd\u4f5c\n1' \u4e34\u754c\u60c5\u51b5\u6709\u4eba\u5728\u5355\u6761\u8bfb\u53d6, \u5199\u65b9\u5c06 nowIndex \u6307\u5411\u4e86 1, \u4f46\u8bfb\u53d6\u7684\u4ecd\u7136\u662f dataConf[0] old\n2' \u4e34\u754c\u60c5\u51b5\u6709\u4eba\u5728\u5355\u6761\u5199\u5165, \u5199\u5165\u7684\u8fd8\u662f dataConf[0] old\n\n\u4e0a\u9762\u884c\u4e3a\u548c\u52a0\u9501\u540e\u4ea7\u51fa\u7ed3\u679c\u4e00\u6837. \u56e0\u800c\u6e05\u9664\u51fd\u6570, \u53ef\u4ee5\u7528\u539f\u5b50\u6280\u5de7\u66ff\u4ee3\u9501.\n\n\u901a\u8fc7\u8fd9\u4e2a\u539f\u7406, \u6211\u4eec\u505a\u914d\u7f6e\u66f4\u65b0\u6216\u8005\u540c\u6b65\u65f6\u5019\u53ef\u4ee5\u91c7\u7528\u4e0b\u9762\u6b65\u9aa4\u83b7\u53d6\u6700\u4f18\u6027\u80fd\n1' \u89e3\u6790\u914d\u7f6e, \u751f\u6210\u4e00\u4e2a\u65b0\u7684\u914d\u7f6e\u5bf9\u8c61 map \u586b\u5145\u5230 dataConf[lastIndex]\n2' \u65b0\u7684\u914d\u7f6e\u5bf9\u8c61\u8bfb\u53d6\u7d22\u5f15\u539f\u5b50\u8d4b\u503c\u7ed9\u5f53\u524d\u7684\u8bfb\u53d6\u7d22\u5f15 lastIndex = lastIndex\n\n\u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u4e48\u591a\u5462. \u56e0\u4e3a\u9501\u662f\u4e00\u4e2a\u6211\u4eec\u9700\u8981\u614e\u91cd\u5bf9\u5f85\u7684\u70b9.\n\n\u800c\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u52a0\u9501, \u4e5f\u6ca1\u6709\u539f\u5b50\u64cd\u4f5c\u7684\u4e52\u4e53\u7ed3\u6784, \u53ef\u4ee5\u81ea\u884c\u5229\u7528 go -race \u5206\u6790. \n\u5176\u8bfb\u5199\u4e00\u81f4\u6027\u65e0\u6cd5\u4fdd\u8bc1(\u8bfb\u5199\u6495\u88c2, \u810f\u8bfb), \u800c\u4e14\u65e0\u6cd5\u4fdd\u8bc1\u7f16\u8bd1\u5668\u4e0d\u505a\u4f18\u5316. \u6709\u65f6\u5019\u90a3\u79cd\u5199\u6cd5\u7ebf\u4e0a\u5c45\u7136\n\u4e0d\u51fa\u95ee\u9898, \u4f46\u662f\u4e00\u65e6\u51fa\u4e86\u95ee\u9898\u5c31\u662f\u83ab\u540d\u5176\u5999, \u5f88\u96be\u8ffd\u67e5. \u8fd9\u91cc\u5c31\u4e0d\u8868\u90a3\u79cd\u9519\u8bef\u7684\u4e52\u4e53\u5199\u6cd5, \u6765\u6c61\u67d3\u540c\n\u884c\u4ee3\u7801.<\/code><\/pre> \n 

4. \u914d\u7f6e\u5e93\u89e3\u6790<\/h3> \n
\u8bf4\u8d77\u914d\u7f6e\u5e93, \u6211\u770b\u6709\u7684\u540c\u5b66\u901a\u8fc7\u8fd9\u6837\u4ee3\u7801\u505a\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u63d0\u53d6\u548c\u5206\u5272.<\/code><\/pre> \n 
content, err := ioutil.ReadFile(file)\nif err != nil {\n    \/\/ ...\n}\n\nfor _, line := range strings.Split(string(content), "\\n") {\n    \/\/ ...\n}<\/code><\/pre> \n 
\u4e0a\u9762\u4ee3\u7801\u5b58\u5728\u4e24\u4e2a\u6f5c\u5728\u95ee\u9898\n1' \u5927\u6587\u4ef6\u5185\u5b58\u4f1a\u70b8\n2' \u4e0d\u540c\u5e73\u53f0\u6362\u884c\u7b26\u4e0d\u7edf\u4e00 mac \\r linux \\n windows \\r\\n\n\n\u4e00\u4e2a\u7a33\u5065\u6f02\u4eae\u4ee3\u7801\u6a21\u677f\u63a8\u8350\u7528\u4e0b\u9762<\/code><\/pre> \n 
    fin, err := os.Open(path)\n    if err != nil {\n        \/\/ Error ...\n    }\n    defer fin.Close()\n\n    \/\/ create a Reader\n    var buf bytes.Buffer\n    reader := bufio.NewReader(fin)\n    for {\n        line, isPrefix, err := reader.ReadLine()\n        if len(line) > 0 {\n            buf.Write(line)\n            if !isPrefix {\n                \/\/ \u5b8c\u6574\u7684\u884c\u5e76\u4e14\u4e0d\u5e26 \\r\\n, \u8fd0\u884c\u72ec\u7acb\u7684\u4e1a\u52a1\u4ee3\u7801 ~\n                lins := string(buf.Bytes())\n\n                buf.Reset()\n            }\n        }\n\n        if err != nil {\n            break\n        }\n    }<\/code><\/pre> \n 
\u5f3a\u70c8\u63a8\u8350!! \u5404\u4f4d\u4fdd\u5b58\u8fd9\u4e2a\u5957\u8def\u6a21\u677f.<\/code><\/pre> \n 

5. Go MD5<\/h3> \n
\u8fd9\u79cd\u9ad8\u9891\u51fa\u73b0\u4ee3\u7801\u7247\u6bb5, \u5f3a\u70c8\u5efa\u8bae\u7edf\u4e00\u5c01\u88c5. \u4fdd\u8bc1\u51fa\u53e3\u7edf\u4e00. \u8fd9\u91cc\u5e26\u5927\u5bb6\u5c01\u88c5\u4e24\u4e2a.<\/code><\/pre> \n 
\/\/ MD5String md5 hash\nfunc MD5String(str string) string {\n    data := md5.Sum([]byte(str))\n    return fmt.Sprintf("%x", data)\n}<\/code><\/pre> \n 
\/\/ MD5File \u6587\u4ef6 MD5\nfunc MD5File(path string) (string, error) {\n    fin, err := os.Open(path)\n    if err != nil {\n        return "", err\n    }\n    defer fin.Close()\n\n    m := md5.New()\n\n    \/\/ \u6587\u4ef6\u8bfb\u53d6\u89e3\u6790, \u5e76\u8bbe\u7f6e\u7f13\u51b2\u7f13\u51b2\u5927\u5c0f\n    const blockS","orderid":"0","title":"golang \u6742\u601d(\u4e00)","smalltitle":"","mid":"0","fname":"GO","special_id":"0","bak_id":"0","info":"0","hits":"282","pages":"3","comments":"0","posttime":"2019-03-31 18:08:16","list":"1554026896","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"golang<\/A> \u6742\u601d<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"47.106.78.186","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"golang \u6742\u601d","lastview":"1714592033","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}