{"id":207420,"date":"2025-07-08T08:41:05","date_gmt":"2025-07-08T00:41:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207420\/"},"modified":"2025-07-08T08:41:05","modified_gmt":"2025-07-08T00:41:05","slug":"%e5%85%b3%e4%ba%8e-unmarshal-yaml-into-struct-%e7%9a%84%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207420\/","title":{"rendered":"\u5173\u4e8e Unmarshal yaml into struct \u7684\u9519\u8bef"},"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>\u5173\u4e8e Unmarshal yaml into struct \u7684\u9519\u8bef<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 14:30:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5173\u4e8e Unmarshal yaml into struct \u7684\u9519\u8bef\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u89e3\u7ec4\u4e00\u4e2a []byte \u53d8\u91cf int struct prometheusyml\u3002\u4ee5\u4e0b\u662f promethuesyml \u548c []byte \u53d8\u91cf\u7684\u5b9a\u4e49\u3002<\/p>\n<pre>type prometheusyml struct {\n        global        global          `yaml:\"global,omitempty\"`\n        scrapeconfigs []scrapeconfigs `yaml:\"scrape_configs,omitempty\"`\n}\n\ntype global struct {\n        scrapeinterval     string `yaml:\"scrape_interval,omitempty\"`\n        evaluationinterval string `yaml:\"evaluation_interval,omitempty\"`\n}\n\ntype scrapeconfigs struct {\n        jobnmaes        string            `yaml:\"job_name,omitempty\"`\n        relabelconfigs  []relabelconfigs  `yaml:\"relabel_configs,omitempty\"`\n        metricspath     string            `yaml:\"metrics_path,omitempty\"`\n        scheme          string            `yaml:\"scheme,omitempty\"`\n        consulsdconfigs []consulsdconfigs `yaml:\"consul_sd_configs,omitempty\"`\n}\n\ntype relabelconfigs struct {\n        sourcelabels string `yaml:\"source_labels,omitempty\"`\n        action       string `yaml:\"action,omitempty\"`\n        regex        string `yaml:\"regex,omitempty\"`\n        replacement  string `yaml:\"replacement,omitempty\"`\n        targetlabel  string `yaml:\"target_label,omitempty\"`\n}\n\ntype consulsdconfigs struct {\n        server   string   `yaml:\"server,omitempty\"`\n        services []string `yaml:\"services,omitempty\"`\n}\n\n# my global config\nglobal:\n  scrape_interval:     15s # set the scrape interval to every 15 seconds. default is every 1 minute.\n  evaluation_interval: 15s # evaluate rules every 15 seconds. the default is every 1 minute.\n  # scrape_timeout is set to the global default (10s).\n\n\nscrape_configs:\n  - job_name: 'consul'\n    relabel_configs:\n        - source_labels:  [\"__meta_consul_service\"]\n          action: replace\n          regex: \"(.*)\"\n          replacement: '${1}'\n          target_label: \"service\"\n        - source_labels: [\"__meta_consul_tags\"]\n          action: replace\n          regex: ',(?:[^,]+,){0}([^=]+)=([^,]+),.*'\n          replacement: '${2}'\n          target_label: '${1}'\n        - source_labels: [\"__meta_consul_tags\"]\n          action: replace\n          regex: ',(?:[^,]+,){1}([^=]+)=([^,]+),.*'\n          replacement: '${2}'\n          target_label: '${1}'\n        - source_labels: [\"__meta_consul_tags\"]\n          action: replace\n          regex: ',(?:[^,]+,){2}([^=]+)=([^,]+),.*'\n          replacement: '${2}'\n          target_label: '${1}'\n    metrics_path: \/metrics\n    scheme: http\n    consul_sd_configs:\n        - server: 192.168.0.101:8500\n          services:\n              - cfs<\/pre>\n<p>\u4f46\u662f\u5f53\u6211\u8fd0\u884c\u8be5\u7a0b\u5e8f\u65f6\u3002\u5b83\u663e\u793a\u4e86\u9519\u8bef\uff0c\u8fd9\u610f\u5473\u7740\u65e0\u6cd5\u5c06 source_labels \u89e3\u7ec4\u5230\u7ed3\u6784\u4e2d\u3002\u5f88\u53ef\u80fd [&#8220;__meta_consul_tags&#8221;] \u65e0\u6cd5\u7ffb\u8bd1\u6210\u5b57\u7b26\u4e32\uff01\uff01\uff01\uff01\u4f46\u6211\u5e94\u8be5\u600e\u4e48\u505a\u624d\u80fd\u4fee\u590d\u8fd9\u4e2a\u9519\u8bef\u5462\uff1f\u5b9e\u9645\u7c7b\u578b\u662f\u4ec0\u4e48\uff1f<\/p>\n<pre>line 11: cannot unmarshal !!seq into string<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><code>relabel_configs<\/code>\u4e2d\u7684<code>source_labels<\/code>\u663e\u7136\u662f<code>string<\/code>\u7684\u6570\u7ec4\u3002\u56e0\u6b64\uff0c\u60a8\u5fc5\u987b\u5c06 <code>sourcelabels<\/code> \u7684 <code> \u6570\u636e\u7c7b\u578b <\/code> \u4ece <code>string<\/code> \u66ff\u6362\u4e3a <code>[]string<\/code>\u3002\u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u4e86\u3002<\/p>\n<pre>type relabelConfigs struct {\n    SourceLabels []string `yaml:\"source_labels,omitempty\"`\n    Action       string   `yaml:\"action,omitempty\"`\n    Regex        string   `yaml:\"regex,omitempty\"`\n    Replacement  string   `yaml:\"replacement,omitempty\"`\n    TargetLabel  string   `yaml:\"target_label,omitempty\"`\n}\n<\/pre>\n<p>\u53ea\u9700\u66f4\u6539\u6b64\u5373\u53ef\u89e3\u51b3\u60a8\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207420","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207420","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=207420"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207420\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}