{"id":42474,"date":"2024-12-01T11:55:46","date_gmt":"2024-12-01T03:55:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42474\/"},"modified":"2024-12-01T11:55:46","modified_gmt":"2024-12-01T03:55:46","slug":"%e6%a0%b9%e6%8d%ae%e5%80%bc%e5%8c%b9%e9%85%8d%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42474\/","title":{"rendered":"\u6839\u636e\u503c\u5339\u914d\u6570\u7ec4"},"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>\u6839\u636e\u503c\u5339\u914d\u6570\u7ec4<\/span><\/p>\n<h1>\u6839\u636e\u503c\u5339\u914d\u6570\u7ec4<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 23:48: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\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6839\u636e\u503c\u5339\u914d\u6570\u7ec4\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u89e3\u6790 yaml\uff0c\u5e76\u5e94\u5c06\u8f93\u51fa\u4f5c\u4e3a <code>runners<\/code> \u5bf9\u8c61\uff0c\u5e76\u4e14\u51fd\u6570 <code>build<\/code> \u5e94\u66f4\u6539\u6570\u636e\u7ed3\u6784\u5e76\u6839\u636e\u4ee5\u4e0b\u7ed3\u6784\u63d0\u4f9b\u8f93\u51fa<\/p>\n<pre>type exec struct {\n    nameval string\n    executer []string\n}<\/pre>\n<p>\u8fd9\u662f\u6211\u5c1d\u8bd5\u8fc7\u7684\uff0c\u4f46\u6211\u4e0d\u77e5\u9053\u5982\u4f55<strong>\u66ff\u6362 \u51fd\u6570\u8fd0\u884c\u5668\u5185\u7684\u786c\u7f16\u7801\u503c\u6765\u81ea\u6211\u5728 yaml \u5185\u83b7\u53d6\u7684\u503c<\/strong><\/p>\n<pre>return []exec{\n    {\"#mytest\",\n        []string{\"spawn child process\", \"build\", \"gulp\"}},\n}<\/pre>\n<p>\u4f7f\u7528\u6765\u81ea <code> \u89e3\u6790\u8fd0\u884c\u5668 <\/code> \u7684\u6570\u636e<\/p>\n<p>\u8fd9\u5c31\u662f\u6211\u5c1d\u8bd5\u8fc7\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u4f60\u77e5\u9053\u5982\u4f55\u505a\u5230\u5417\uff1f <\/p>\n<pre>package main\n\nimport (\n    \"log\"\n\n    \"gopkg.in\/yaml.v2\"\n)\n\nvar runContent = []byte(`\napi_ver: 1\nrunners:\n  - name: function1\n    data: mytest\n    type:\n    - command: spawn child process\n    - command: build\n    - command: gulp\n  - name: function2\n    data: mytest2\n    type:\n    - command: webpack\n  - name: function3\n    data: mytest3\n    type:\n    - command: ruby build\n  - name: function4\n    type:\n  - command: go build\n`)\n\ntype Result struct {\n    Version string    `yaml:\"api_ver\"`\n    Runners []Runners `yaml:\"runners\"`\n}\n\ntype Runners struct {\n    Name string    `yaml:\"name\"`\n    Type []Command `yaml:\"type\"`\n}\n\ntype Command struct {\n    Command string `yaml:\"command\"`\n}\n\nfunc main() {\n\n    var runners Result\n    err := yaml.Unmarshal(runContent, &amp;runners)\n    if err != nil {\n        log.Fatalf(\"Error : %v\", err)\n    }\n\n    \/\/Here Im calling to the function with the parsed structured data  which need to return the list of Exec\n    build(\"function1\", runners)\n\n}\n\ntype Exec struct {\n    NameVal  string\n    Executer []string\n}\n\nfunc build(name string, runners Result) []Exec {\n\n    for _, runner := range runners.Runners {\n\n        if name == runner.Name {\n            return []Exec{\n                \/\/ this just for example, nameVal and Command\n                {\"# mytest\",\n                    []string{\"spawn child process\", \"build\", \"gulp\"}},\n            }\n        }\n    }\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5c06 runners \u5bf9\u8c61\u7684\u540d\u79f0\u5206\u914d\u7ed9 struct <code>exec<\/code> \u5b57\u6bb5\u4f5c\u4e3a\u540d\u79f0\uff0c\u5e76\u5c06\u547d\u4ee4\u5217\u8868\u9644\u52a0\u5230 <code>[]string<\/code> \u7c7b\u578b\u5b57\u6bb5\uff0c\u5176\u4e2d\u5305\u542b\u4e0e\u540d\u79f0\u5339\u914d\u7684\u51fd\u6570\u7684\u547d\u4ee4\uff1a<\/p>\n<pre>func build(name string, runners Result) []Exec {\n    exec := make([]Exec, len(runners.Runners))\n    for i, runner := range runners.Runners {\n\n        if name == runner.Name {\n            exec[i].NameVal = runner.Name\n            for _, cmd := range runner.Type {\n                exec[i].Executer = append(exec[i].Executer, cmd.Command)\n            }\n            fmt.Printf(\"%+v\", exec)\n            return exec\n        }\n    }\n    return exec\n}<\/pre>\n<p> \u4e0a\u7684\u5de5\u4f5c\u4ee3\u7801<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u6839\u636e\u503c\u5339\u914d\u6570\u7ec4\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u6839\u636e\u503c\u5339\u914d\u6570\u7ec4 \u6839\u636e\u503c\u5339\u914d\u6570\u7ec4 \u6765\u6e90\uff1astackoverflow 2024-04-23 23:48:35 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300a\u6839\u636e\u503c\u5339\u914d\u6570\u7ec4\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u89e3\u6790 yaml\uff0c\u5e76\u5e94\u5c06\u8f93\u51fa\u4f5c\u4e3a runners \u5bf9\u8c61\uff0c\u5e76\u4e14\u51fd\u6570 build \u5e94\u66f4\u6539\u6570\u636e\u7ed3\u6784\u5e76\u6839\u636e\u4ee5\u4e0b\u7ed3\u6784\u63d0\u4f9b\u8f93\u51fa type exec struct { nameval string executer []string } \u8fd9\u662f\u6211\u5c1d\u8bd5\u8fc7\u7684\uff0c\u4f46\u6211\u4e0d\u77e5\u9053\u5982\u4f55\u66ff\u6362 \u51fd\u6570\u8fd0\u884c\u5668\u5185\u7684\u786c\u7f16\u7801\u503c\u6765\u81ea\u6211\u5728 yaml \u5185\u83b7\u53d6\u7684\u503c return []exec{ {&#8220;#mytest&#8221;, []string{&#8220;spawn child process&#8221;, &#8220;build&#8221;, &#8220;gulp&#8221;}}, } \u4f7f\u7528\u6765\u81ea \u89e3\u6790\u8fd0\u884c\u5668 \u7684\u6570\u636e \u8fd9\u5c31\u662f\u6211\u5c1d\u8bd5\u8fc7\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u4f60\u77e5\u9053\u5982\u4f55\u505a\u5230\u5417\uff1f package main import ( &#8220;log&#8221; &#8220;gopkg.in\/yaml.v2&#8221; ) var runContent [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42474","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42474"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42474\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}