{"id":207617,"date":"2025-07-08T08:43:40","date_gmt":"2025-07-08T00:43:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207617\/"},"modified":"2025-07-08T08:43:40","modified_gmt":"2025-07-08T00:43:40","slug":"%e5%b0%86%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%94%a8%e4%bd%9c%e7%bb%93%e6%9e%84%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207617\/","title":{"rendered":"\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a\u7528\u4f5c\u7ed3\u6784\u5b57\u6bb5"},"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\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a\u7528\u4f5c\u7ed3\u6784\u5b57\u6bb5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 23:33:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a\u7528\u4f5c\u7ed3\u6784\u5b57\u6bb5\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u6253\u5370\u4e00\u4e2a json\uff08\u8f6c\u6362\u4e3a\u7ed3\u6784\uff09\u5b57\u6bb5\uff0c\u7279\u522b\u662f\u4f5c\u4e3a\u6765\u81ea\u7ec8\u7aef\u7684\u8f93\u5165\u3002<\/p>\n<pre>type foo struct{ \nfield1 string\nfield2 int\n}<\/pre>\n<p>\u8fd9\u662f\u6211\u521b\u5efa\u7684\u7ed3\u6784\u793a\u4f8b\uff0c\u6211\u60f3\u5c06\u201cfield1\u201d\u4f5c\u4e3a\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u901a\u8fc7\u5230\u8fbe\u89e3\u7801\u7684 json \u8f93\u51fa\u5c06\u5176\u6253\u5370\u5230 html \u6b63\u6587\u6216\u63a7\u5236\u53f0\u3002<\/p>\n<p>\u901a\u5e38\u6211\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u5f0f\u6253\u5370\u5b83\uff0c\u4f46\u6211\u627e\u4e0d\u5230\u5c06 <strong>os.args[1]<\/strong> \u503c\u8f6c\u6362\u4e3a\u5df2\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>var res *foo\n_=json.newdecoder(resp.body).decode(&amp;res)\nfmt.println(res.field1)<\/pre>\n<p>\u603b\u4e4b\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u5c06\u4efb\u4f55\u5b57\u7b26\u4e32\u53d8\u91cf\u8f6c\u6362\u4e3a\u51fd\u6570\/\u65b9\u6cd5\u3002\u63d0\u524d\u975e\u5e38\u611f\u8c22<\/p>\n<p>\u7267\u5e08\u3002\u6ce8\u610f\uff1a\u4f8b\u5982\u3002 \u6211\u5c06\u5199\u5165\u7ec8\u7aef\uff1a<\/p>\n<pre>go run main.go \"field1\"<\/pre>\n<p>\u7a0b\u5e8f\u4f1a\u505a\u8fd9\u6837\u7684\u4e8b\u60c5<\/p>\n<pre>fmt.Fprintf(writer, \"option.field is %v\", someFuncThatConcatenatesThisTwo(\"option.\"+ os.Args[1]))<\/pre>\n<p>\u987a\u4fbf\u8bf4\u4e00\u53e5\uff0c\u6709\u591a\u4e2a\u5b57\u6bb5\u9700\u8981\u8c03\u7528\u3002\u9884\u5b9a\u4e49\u5f53\u7136\u53ef\u4ee5\u89e3\u51b3\u7279\u5b9a\u60c5\u51b5\uff0c\u4f46\u6211\u60f3\u5b66\u4e60\u7684\u662f\u5176\u4ed6\u4e1c\u897f\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u603b\u4e4b\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u5c06\u4efb\u4f55\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u51fd\u6570\/\u65b9\u6cd5\u3002<\/p>\n<p>\u6211\u4e0d\u786e\u5b9a\u60a8\u60f3\u5728\u8fd9\u91cc\u5b9e\u73b0\u4ec0\u4e48\u76ee\u6807\u3002\u8fd9\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u8981\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u586b\u5145\u7ed3\u6784\u5b57\u6bb5\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u5982\u4e0b\u64cd\u4f5c\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"strconv\"\n    \"os\"\n)\n\ntype options struct {\n    field1 string\n    field2 int64\n}\n\nfunc main() {\n    if len(os.args) &lt; 2 {\n        log.fatal(\"missing two required positional arguments: field1 and field2\")\n    }\n\n    opts := &amp;options{}\n    opts.field1 = os.args[1]\n\n    var err error\n    opts.field2, err = strconv.parseint(os.args[2], 10, 64)\n    if err != nil {\n        log.fatalf(\"failed to parse integer value: %v\", os.args[2])\n    }\n\n    fmt.println(opts)\n}\n<\/pre>\n<p>\u4e3a\u4e86\u8ba9\u60a8\u7684\u751f\u6d3b\u53d8\u5f97\u66f4\u52a0\u8f7b\u677e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \uff08\u6216 \uff09\u5305\u5c06\u8f93\u5165\u53c2\u6570\u58f0\u660e\u4e3a\u547d\u4ee4\u884c\u6807\u5fd7\u3002<\/p>\n<pre>import (\n    \"flag\"\n    \"fmt\"\n)\n\ntype options struct {\n    field1 string\n    field2 int\n}\n\nvar opts options\n\nfunc init() {\n    flag.stringvar(&amp;opts.field1, \"field1\", \"\", \"help message for field1\")\n    flag.intvar(&amp;opts.field2, \"field2\", 0, \"help message for field2\")\n    flag.parse()\n}\n\nfunc main() {\n   fmt.println(opts)\n}\n<\/pre>\n<p>\u6216\u8005\uff0c\u5c31\u50cf @jomaar \u56de\u7b54\u7684\u90a3\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u50cf \u8fd9\u6837\u7684\u5e2e\u52a9\u7a0b\u5e8f\u5e93\u6765\u907f\u514d\u624b\u52a8\u58f0\u660e\u547d\u4ee4\u884c\u6807\u5fd7\u3002\u53e6\u4e00\u79cd\u9009\u62e9\u662f \u3002<\/p>\n<h3>\u7f16\u8f91<\/h3>\n<p>\u7ecf\u8fc7\u8fdb\u4e00\u6b65\u6f84\u6e05\uff0c\u60a8\u4f3c\u4e4e\u5e0c\u671b\u4f7f\u7528\u7f16\u5199\u5668\u6709\u9009\u62e9\u5730\u5199\u5165\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\uff0c\u5e76\u4e14\u5e0c\u671b\u4f7f\u7528\u4f4d\u7f6e\u547d\u4ee4\u884c\u53c2\u6570\u6765\u6307\u5b9a\u8981\u5199\u5165\u7684\u5b57\u6bb5\u3002<\/p>\n<p>\u6211\u8ba4\u4e3a <code>map<\/code> \u5c06\u662f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u66f4\u9002\u5408\u5b58\u50a8\u9009\u9879\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u7b80\u5355\u5730\u4f7f\u7528 <code>string<\/code> \u952e\u5f15\u7528\u5b57\u6bb5\u3002<\/p>\n<pre>import (\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    options := map[string]interface{} {\n        \"field1\": \"some-value\",\n        \"field2\": 1,\n    }\n\n    for _, arg := range os.args[1:] {\n        if val, ok := options[arg]; ok {\n            fmt.println(val)\n        }\n    }\n}\n<\/pre>\n<p>\u5982\u679c\u60a8\u60f3\u7ee7\u7eed\u4f7f\u7528\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u4f7f\u7528 \u5305\u3002<\/p>\n<pre>import (\n    \"fmt\"\n    \"os\"\n    \"reflect\"\n)\n\ntype Options struct {\n    Field1 string\n    Field2 int\n}\n\nfunc main() {\n    opts := &amp;Options{Field1: \"some-value\", Field2: 1}\n\n    for _, arg := range os.Args[1:] {\n        fmt.Println(getAttr(opts, arg))\n    }\n}\n\n\/\/ copied from https:\/\/stackoverflow.com\/a\/66470232\/2410641\nfunc getAttr(obj interface{}, fieldName string) (reflect.Value, error) {\n    pointToStruct := reflect.ValueOf(obj) \/\/ addressable\n    curStruct := pointToStruct.Elem()\n    if curStruct.Kind() != reflect.Struct {\n        return reflect.Value{}, fmt.Errorf(\"obj is not a struct\")\n    }\n    curField := curStruct.FieldByName(fieldName) \/\/ type: reflect.Value\n    if !curField.IsValid() {\n        return reflect.Value{}, fmt.Errorf(\"field not found: %s\", fieldName)\n    }\n    return curField, nil\n}\n<\/pre>\n<p>go\u6f14\u793a\uff1a<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207617","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207617","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=207617"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207617\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}