{"id":208142,"date":"2025-07-08T08:44:39","date_gmt":"2025-07-08T00:44:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208142\/"},"modified":"2025-07-08T08:44:39","modified_gmt":"2025-07-08T00:44:39","slug":"%e6%a3%80%e6%9f%a5%e5%87%bd%e6%95%b0%e6%98%af%e5%90%a6%e4%bc%9a%e8%b0%83%e7%94%a8%e5%8f%a6%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208142\/","title":{"rendered":"\u68c0\u67e5\u51fd\u6570\u662f\u5426\u4f1a\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570"},"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>\u68c0\u67e5\u51fd\u6570\u662f\u5426\u4f1a\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 15:18: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>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u68c0\u67e5\u51fd\u6570\u662f\u5426\u4f1a\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u613f\u610f\u4e3a\u6211\u7684 go \u5e94\u7528\u7a0b\u5e8f\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<p>\u6709\u4e00\u4e2a\u51fd\u6570\u4f1a\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u8be5\u5982\u4f55\u786e\u8ba4\u8fd9\u4e2a\u8c03\u7528\uff1f<\/p>\n<pre>\/\/ the function which I wanna test\nfunc big(t int) {\n\n    bang(t * 6) \/\/ how to confirm this? \n}<\/pre>\n<p>\u6211\u65e0\u6cd5\u6a21\u62df <code>bang()<\/code>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u7ed3\u6784\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u7b80\u800c\u8a00\u4e4b\uff0c\u4f60\u6ca1\u6709\u3002\u5982\u679c\u9700\u8981\u9a8c\u8bc1\uff0c\u60a8\u53ef\u4ee5\u5728\u5b83\u81ea\u5df1\u7684\u5355\u5143\u6d4b\u8bd5\u4e2d\u6d4b\u8bd5 <code>bang<\/code> \u3002\u7136\u800c\uff0c\u62e5\u6709\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u662f\u5f88\u68d8\u624b\u7684\u2014\u2014\u4f60\u53ea\u80fd\u6d4b\u8bd5\u5b83\u4eec\u4e0e\u67d0\u4e9b\u4e1c\u897f\u4ea4\u4e92\u65f6\u7684\u884c\u4e3a\u3002<\/p>\n<p>tl;dr\uff1a\u5982\u679c\u60a8\u9700\u8981\u6d4b\u8bd5\u884c\u4e3a\uff0c\u8bf7\u5c3d\u53ef\u80fd\u7b80\u5355\u5730\u5b9e\u9645\u6d4b\u8bd5\u884c\u4e3a\u3002\u7279\u522b\u662f\u5f53\u60a8\u6d4b\u8bd5\u884c\u4e3a\u65f6\uff0c\u8bf7\u5f7b\u5e95\u6d4b\u8bd5\uff1a\u5b83\u4f7f\u60a8\u7684\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n<h2>\u793a\u4f8b<\/h2>\n<p>\u4e00\u4e2a\u201c\u66f4\u597d\u201d\uff08\u9605\u8bfb\uff1a\u66f4\u5177\u4f53\u548c\u900f\u660e\uff09\u7684\u793a\u4f8b\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func hello(lang string) string {\n    switch lang {\n    case \"de\":\n        return \"hallo\"\n    case \"es\":\n        return \"hola\"\n    default:\n        return \"hello\"\n    }\n}\n\nfunc world(lang string) string {\n    switch lang {\n    case \"de\":\n        return \"welt\"\n    case \"es\":\n        return \"mundo\"\n    default:\n        return \"world\"\n    }\n}\n\nfunc greet(lang string) {\n    var g string\n    switch lang {\n    case \"de\":\n        g = fmt.sprintf(\"%s, %s!\", hello(\"de\"), world(\"de\"))\n    case \"es\":\n        g = fmt.sprintf(\"\u00a1%s, %s!\", hello(\"es\"), world(\"es\"))\n    default:\n        g = fmt.sprintf(\"%s, %s!\", hello(lang), world(lang))\n    }\n\n    fmt.println(g)\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u51fd\u6570 <code>hello<\/code> \u548c <code>world<\/code> \u5f88\u5bb9\u6613\u6d4b\u8bd5\u3002\u4ee5\u4f60\u597d\u4e3a\u4f8b\uff1a<\/p>\n<pre>\/\/ testhello is heavily simplified for brevity.\nfunc testhello(t *testing.t) {\n    testcases := []struct {\n        desc     string\n        lang     string\n        expected string\n    }{\n        {\n            desc:     \"german\",\n            lang:     \"de\",\n            expected: \"hallo\",\n        },\n        {\n            desc:     \"spanish\",\n            lang:     \"es\",\n            expected: \"hola\",\n        },\n        {\n            desc:     \"default\",\n            lang:     \"en\",\n            expected: \"hello\",\n        },\n    }\n    for _, tc := range testcases {\n        t.run(tc.desc, func(t *testing.t) {\n            if hello(tc.lang) != tc.expected {\n                t.fail()\n            }\n        })\n    }\n}<\/pre>\n<p>\u5bf9 <code>world<\/code> \u91cd\u590d\u6b64\u64cd\u4f5c\uff0c\u60a8\u5c31\u4f1a\u77e5\u9053\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u884c\u4e3a\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u9884\u671f\u3002<\/p>\n<p>\u4f46\u662f\u60a8\u5c06\u5982\u4f55\u6d4b\u8bd5 <code>greet<\/code> \u5462\uff1f\u5b83\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u5728\u5f53\u524d\u5f62\u5f0f\u4e0b\uff0c\u786e\u4fdd\u5199\u5165\u5185\u5bb9\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u5b9e\u9645\u91cd\u5b9a\u5411 <code>os.stdout<\/code> \u7684\u8f93\u51fa\u3002\u8fd9\u53ef\u80fd\u5c31\u8db3\u591f\u4e86\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u7684\u7528\u4f8b\uff0c\u4f46\u5b83\u5f88\u9ebb\u70e6\u4e14\u5197\u957f\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u66f4\u6709\u610f\u4e49\uff1a<\/p>\n<pre>\/\/ dsts is variadic to make it optional.\n\/\/ so you can either call it as greet(\"en\")\n\/\/ or greet(\"en\",whatever), with the added bonus\n\/\/ that you can write to multiple destinations.\nfunc greet(lang string, dsts ...io.writer) {\n\n    var g string\n    var out io.writer\n\n    if dsts == nil {\n        \/\/ if nothing is set, we want the default behavior.\n        out = os.stdout\n    } else if len(dsts) == 1 {\n        \/\/ if only one dst is set for example for unit tests *wink* *wink*\n        \/\/ we can write to it directly.\n        out = dsts[0]\n    } else {\n        \/\/ in case multiple dsts are set, we utilize multiwriter\n        out = io.multiwriter(dsts...)\n    }\n\n    switch lang {\n    case \"de\":\n        g = fmt.sprintf(\"%s, %s!\", hello(\"de\"), world(\"de\"))\n    case \"es\":\n        g = fmt.sprintf(\"\u00a1%s, %s!\", hello(\"es\"), world(\"es\"))\n    default:\n        g = fmt.sprintf(\"%s, %s!\", hello(lang), world(lang))\n    }\n    fmt.fprintln(out, g)\n}<\/pre>\n<p>\u6d4b\u8bd5\u53d8\u5f97\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<pre>func TestGreet(t *testing.T) {\n\n    buf := bytes.NewBuffer(nil)\n\n    for _, lang := range []string{\"de\", \"es\", \"unknown\"} {\n        greet(lang, buf)\n        greeting := buf.String()\n\n        \/\/ Of course, testing should be a bit more thorough\n        if greeting == \"\" {\n            t.Errorf(\"greeting for %s is empty!\", lang)\n        }\n\n        if !strings.HasSuffix(greeting, \"!\\n\") {\n            t.Errorf(\"greeting '%s' for language '%s' has no exclamation mark or newline\", greeting, lang)\n        }\n    }\n\n}<\/pre>\n<p>\u66f4\u9ad8\u7ea7\u7684\u6280\u672f\uff08\u4f8b\u5982\u6570\u636e\u5e93\u8fde\u63a5\uff09\u5c06\u662f \u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u68c0\u67e5\u51fd\u6570\u662f\u5426\u4f1a\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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-208142","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208142","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=208142"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208142\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}