{"id":207067,"date":"2025-07-08T10:55:50","date_gmt":"2025-07-08T02:55:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207067\/"},"modified":"2025-07-08T10:55:50","modified_gmt":"2025-07-08T02:55:50","slug":"%e5%b0%86%e9%85%8d%e7%bd%ae%e7%bb%93%e6%9e%84%e5%8a%a0%e8%bd%bd%e5%88%b0-go-%e4%b8%ad%e5%90%8e%ef%bc%8c%e8%ae%bf%e9%97%ae%e5%b5%8c%e5%a5%97%e5%b1%9e%e6%80%a7%e6%97%b6%e5%87%ba%e7%8e%b0%e9%97%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207067\/","title":{"rendered":"\u5c06\u914d\u7f6e\u7ed3\u6784\u52a0\u8f7d\u5230 go \u4e2d\u540e\uff0c\u8bbf\u95ee\u5d4c\u5957\u5c5e\u6027\u65f6\u51fa\u73b0\u95ee\u9898"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5c06\u914d\u7f6e\u7ed3\u6784\u52a0\u8f7d\u5230 go \u4e2d\u540e\uff0c\u8bbf\u95ee\u5d4c\u5957\u5c5e\u6027\u65f6\u51fa\u73b0\u95ee\u9898<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 20:18:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5c06\u914d\u7f6e\u7ed3\u6784\u52a0\u8f7d\u5230 go \u4e2d\u540e\uff0c\u8bbf\u95ee\u5d4c\u5957\u5c5e\u6027\u65f6\u51fa\u73b0\u95ee\u9898\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6240\u4ee5\u6211\u60f3\u5c06 <code>config.yaml<\/code> \u6587\u4ef6\u52a0\u8f7d\u5230\u6211\u7684\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<pre>defaulttimeout: 0.25\ndefaultworkspace: ibm\nworkspaces:\n  ibm:\n    client: 47943938\n    defaultgoal: 27.5\n    projects:\n      - 159929907\n      - 160046030\n      - 160064503\n  pp:\n    defaultgoal: 5.5\n    project: 160064503\n<\/pre>\n<p>\u56e0\u6b64\u6211\u521b\u5efa\u4ee5\u4e0b\u7ed3\u6784\uff1a<\/p>\n<pre>type config struct {\n    defaulttimeout   float64 `yaml:\"defaulttimeout\"`\n    defaultworkspace string  `yaml:\"defaultworkspace\"`\n    workspaces       struct {\n        ibm struct {\n            client      int     `yaml:client`\n            defaultgoal float64 `yaml:\"defaultgoal\"`\n            projects    []int   `yaml:\"projects\"`\n        }\n        pp  struct {\n            defaultgoal float64 `yaml:\"defaultgoal\"`\n            project     int     `yaml:project`\n        }\n    }\n}\n<\/pre>\n<p>\u57fa\u672c\u4e0a\uff0c\u6211\u5217\u51fa\u4e86\u4e0d\u540c\u7684\u5de5\u4f5c\u533a\uff0c\u5176\u4e2d\u5305\u542b\u4e0d\u540c\u7684\u5c5e\u6027\u548c\u4e00\u4e2a\u5171\u540c\u5c5e\u6027\uff1a<code>defaultgoal<\/code>\u3002\u73b0\u5728\u6211\u60f3\u4ee5\u4e00\u79cd\u5e72\u51c0\u6709\u6548\u7684\u65b9\u5f0f\u8bbf\u95ee\u8fd9\u4e2a\u5c5e\u6027\u3002<\/p>\n<p>\u73b0\u5728\u6211\u6709\u4e86\u53c2\u6570 <code>*workspacename<\/code> \u6765\u51b3\u5b9a\u6211\u8981\u9009\u62e9\u54ea\u4e2a\u5de5\u4f5c\u7a7a\u95f4\u3002\u6240\u4ee5\u95ee\u9898\u662f\u6211\u5982\u4f55\u4f7f\u7528\u5b83\u4f5c\u4e3a\u6211\u60f3\u8981\u9009\u62e9\u7684 <code>workspace<\/code> \u7684\u952e\uff0c\u7136\u540e\u8fdb\u4e00\u6b65\u83b7\u53d6\u76f8\u5e94\u6761\u76ee\u7684 <code>defaultgoal<\/code> \uff1f<\/p>\n<p>\u6211\u5c1d\u8bd5\u4e86\u4ee5\u4e0b\u64cd\u4f5c\uff08<code>config<\/code> \u662f\u4ece yaml \u6e90\u52a0\u8f7d\u7684\u7ed3\u6784\uff09<\/p>\n<pre>wsConf := reflect.Indirect(reflect.ValueOf(config.Workspaces)).FieldByName(*workspaceName)<\/pre>\n<p>\u4f46\u662f\u6211\u6709 <code>reflect.value<\/code> \uff0c\u6211\u5fc5\u987b\u5c06\u5176\u8f6c\u6362\u56de\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u8bc4\u4f30 <code>defaultgoal<\/code> \u3002 <\/p>\n<p>\u8fd9\u91cc\u6709\u4ec0\u4e48\u597d\u7684\u65b9\u6cd5\u5417\uff1f\u5982\u6709\u4efb\u4f55\u5e2e\u52a9\uff0c\u6211\u4eec\u5c06\u4e0d\u80dc\u611f\u6fc0\uff01<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4e0e\u4f7f\u7528\u53cd\u5c04\uff08<code>reflect<\/code> \u5305\uff09\u76f8\u6bd4\uff0c\u4f7f\u7528 <code>map[string]interface{}<\/code> \u4f5c\u4e3a\u5b57\u6bb5\u7c7b\u578b\uff08\u7279\u522b\u662f <code>workspaces<\/code> \u5b57\u6bb5\uff09\uff0c\u7136\u540e\u5c06\u6b64\u5730\u56fe\u4e2d\u7684\u9879\u76ee\u89c6\u4e3a\u7279\u5b9a\u5de5\u4f5c\u533a\u53ef\u80fd\u4f1a\u66f4\u5bb9\u6613\u3002<\/p>\n<pre>type config struct {\n    defaulttimeout   float64 `yaml:\"defaulttimeout\"`\n    defaultworkspace string  `yaml:\"defaultworkspace\"`\n    workspaces       map[string]interface{} `yaml: workspaces`\n}\n\ntype ibm struct {\n            client      int     `yaml:client`\n            defaultgoal float64 `yaml:\"defaultgoal\"`\n            projects    []int   `yaml:\"projects\"`\n}\ntype pp struct {\n            defaultgoal int `yaml:\"defaultgoal\"`\n            project     int `yaml:project`\n}\n\nfunc main() {\n    var cfg config\n    \/\/ init cfg somehow\n    for space, value := range cfg.workspaces {\n        switch space {\n        case \"ibm\": \/\/ convert `value` to ibm struct\n        case \"pp\": \/\/ convert `value` to pp struct\n    }\n}<\/pre>\n<p>\u5982\u679c\u60a8\u60f3\u6839\u636e <code>workspace<\/code> \u540d\u79f0\u89e3\u7ec4\u4e3a\u7279\u5b9a\u7ed3\u6784\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u6b64\u65b9\u6cd5\u3002 \uff08\u4f7f\u7528json\u65f6\u4e00\u822c\u4e0d\u9700\u8981\u989d\u5916\u7684marshal\u6b65\u9aa4\u3002\u66f4\u591a\uff09\u3002\u5de5\u4f5c\u793a\u4f8b<\/p>\n<pre>package main\n\nimport (\n    \"log\"\n\n    \"gopkg.in\/yaml.v2\"\n)\n\ntype Config struct {\n    DefaultTimeout   float64 `yaml:\"defaultTimeout\"`\n    DefaultWorkspace string  `yaml:\"defaultWorkspace\"`\n    Workspaces       map[string]interface{}\n}\n\ntype PP struct {\n    DefaultGoal int `yaml:\"defaultGoal\"`\n    Project     int `yaml:\"project\"`\n}\n\ntype IBM struct {\n    Client      int     `yaml:\"client\"`\n    DefaultGoal float64 `yaml:\"defaultGoal\"`\n    Projects    []int   `yaml:\"projects\"`\n}\n\nfunc main() {\n    yamlData := `defaultTimeout: 0.25\ndefaultWorkspace: ibm\nworkspaces:\nibm:\n    client: 47943938\n    defaultGoal: 27.5\n    projects:\n    - 159929907\n    - 160046030\n    - 160064503\npp:\n    defaultGoal: 5.5\n    project: 160064503`\n\n    var c Config\n    if err := yaml.Unmarshal([]byte(yamlData), &amp;c); err != nil {\n        panic(err)\n    }\n\n    for space, value := range c.Workspaces {\n        bs, err := yaml.Marshal(value)\n        if err != nil {\n            panic(err)\n        }\n\n        switch space {\n        case \"ibm\":\n            var i IBM\n            if err := yaml.Unmarshal(bs, &amp;i); err != nil {\n                panic(err)\n            }\n\n            log.Printf(\"IBM value: %v\", i)\n\n        case \"pp\":\n            var p PP\n            if err := yaml.Unmarshal(bs, &amp;p); err != nil {\n                panic(err)\n            }\n\n            log.Printf(\"PP value: %v\", p)\n        }\n    }\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5c06\u914d\u7f6e\u7ed3\u6784\u52a0\u8f7d\u5230 go \u4e2d\u540e\uff0c\u8bbf\u95ee\u5d4c\u5957\u5c5e\u6027\u65f6\u51fa\u73b0\u95ee\u9898\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207067","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207067"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207067\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}