
{"id":1666,"date":"2022-06-18T23:34:58","date_gmt":"2022-06-18T15:34:58","guid":{"rendered":"https:\/\/www.changxuan.top\/?p=1666"},"modified":"2022-06-19T10:59:18","modified_gmt":"2022-06-19T02:59:18","slug":"golang-%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.changxuan.top\/?p=1666","title":{"rendered":"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u524d\u8a00<\/h3>\n\n\n\n<p>\u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u6bb5\u5728\u5e94\u7528\u7a0b\u5e8f\u6216\u5305\u4e2d\u7528\u4e8e\u6d4b\u8bd5\u7a0b\u5e8f\u529f\u80fd\u662f\u5426\u6b63\u786e\u7684\u51fd\u6570\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6709\u7740\u5341\u5206\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5f00\u59cb<\/h3>\n\n\n\n<p>\u5728\u4e00\u4e9b\u4f7f\u7528 <code>golang<\/code> \u7f16\u5199\u7684\u5f00\u6e90\u8f6f\u4ef6\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u770b\u5230\u4ee5 <code>_test.go<\/code> \u7ed3\u5c3e\u7684\u6587\u4ef6\u540d\u3002\u8fd9\u4e9b\u90fd\u662f\u5f00\u53d1\u8005\u9488\u5bf9\u81ea\u5df1\u7684\u529f\u80fd\u4ee3\u7801\u6240\u7f16\u5199\u7684\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\uff0c\u5b83\u4eec\u901a\u5e38\u4f1a\u548c\u88ab\u6d4b\u8bd5\u7684\u4ee3\u7801\u653e\u7f6e\u5728\u540c\u4e00\u4e2a\u5305\u4e0b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step1 \u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u4ecb\u7ecd golang \u4e2d\u5355\u5143\u6d4b\u8bd5\u6240\u6d89\u53ca\u7684\u51e0\u4e2a\u6982\u5ff5\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u5728\u8fd9\u91cc\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.<br>\u251c\u2500\u2500 math.go<br>\u2514\u2500\u2500 math_test.go<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ math.go \u529f\u80fd\u4ee3\u7801<br>package math<br>\u200b<br>func Add(a, b int) int {<br>  return a + b<br>}<br>\u200b<br>func Sub(a, b int) int {<br>  return a - b<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ math_test.go\uff08v1\uff09\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801<br>package math<br>\u200b<br>import \"testing\"<br>\u200b<br>func TestAdd(t *testing.T) {<br>  if Add(1, 2) != 3 {<br>    t.Error(\"Add(1, 2) != 3\")<br>  }<br>}<\/pre>\n\n\n\n<p>\u5728 <code>math_test.go<\/code> \u4e2d\u7684\u5355\u5143\u6d4b\u8bd5\u51fd\u6570\u7b7e\u540d\u98ce\u683c\u4e3a <code>func TestXxxx(t *testing.T)<\/code> \uff0c\u8be5\u7c7b\u578b\u51fd\u6570\u6709\u4e14\u53ea\u80fd\u6709\u4e00\u4e2a\u53c2\u6570 <code>t *testing.T<\/code>\uff0c\u8fd9\u662f Go \u8bed\u8a00\u5355\u5143\u6d4b\u8bd5\u7684\u4e00\u79cd\u89c4\u8303\u3002\u4e0d\u4ec5\u5982\u6b64\uff0c\u6240\u6709\u7684\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u7684\u547d\u540d\u4e5f\u5fc5\u987b\u9075\u5faa <code>xxxx_test.go<\/code> \u7684\u683c\u5f0f\u3002\u53ea\u6709\u8fd9\u6837\uff0c\u5728\u6267\u884c <code>go test<\/code> \u547d\u4ee4\u65f6\u624d\u53ef\u4ee5\u5206\u8fa8\u51fa\u54ea\u4e9b\u662f\u9700\u8981\u6267\u884c\u7684\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step2 \u8fd0\u884c\u5355\u5143\u6d4b\u8bd5<\/h4>\n\n\n\n<p>\u5728\u5305\u542b\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u7684\u76ee\u5f55\u4e0b\u6267\u884c\u547d\u4ee4 <code>go test -v<\/code> \uff08-v \uff1a \u6253\u5370\u8fd0\u884c\u7ec6\u8282\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u52a0 -v\uff09\uff0c\u53ef\u4ee5\u770b\u5230\u63a7\u5236\u53f0\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">=== RUN &nbsp; TestAdd<br>--- PASS: TestAdd (0.00s)<br>PASS<br>ok &nbsp; &nbsp;  demo-unit-test\/math &nbsp; &nbsp; 0.513s<\/pre>\n\n\n\n<p><code>go test<\/code> \u547d\u4ee4\u53ea\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u5bfb\u627e\u4ee5 <code>_test.go<\/code> \u4e3a\u540e\u7f00\u7684\u6587\u4ef6\uff0c\u5982\u679c\u60f3\u8981\u4e00\u5e76\u6267\u884c\u5f53\u524d\u76ee\u5f55\u4e0b\u4ee5\u53ca\u5f53\u524d\u76ee\u5f55\u7684\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4 <code>go test .\/...<\/code>\u3002<code>go test<\/code> \u4f1a\u53bb\u626b\u63cf\u8fd9\u4e9b\u6587\u4ef6\u4e2d\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u5305\u62ec <code>func TestXxxx<\/code> \u548c\u5176\u5b83\u51e0\u79cd\u51fd\u6570\u3002\u63a5\u7740 <code>go test<\/code> \u4f1a\u751f\u6210\u4e00\u4e2a\u4e34\u65f6\u7684\u4e0d\u53ef\u89c1\u7684 <code>main<\/code> \u5305\u6765\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u6784\u5efa\u5e76\u8fd0\u884c\u5b83\u4eec\u3002\u53e6\u5916\uff0c\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f <code>go test<\/code> \u5e76\u4e0d\u4fdd\u8bc1\u6bcf\u4e2a <code>package<\/code> \u4e4b\u95f4\u5355\u5143\u6d4b\u8bd5\u7684\u987a\u5e8f\uff0c\u901a\u5e38\u5b83\u4eec\u4f1a\u662f\u5e76\u884c\u7684\u3002\u6240\u4ee5\uff0c\u5728\u7f16\u5199 <code>package<\/code> \u5185\u7684\u5355\u5143\u6d4b\u8bd5\u65f6\u4e0d\u8981\u4f9d\u8d56\u5176\u4ed6\u5305\u5185\u7684\u6570\u636e\u6216\u8005\u6267\u884c\u7ed3\u679c\u3002\u6700\u7ec8\u5c06\u751f\u6210\u6d4b\u8bd5\u7684\u7ed3\u679c\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\uff0c\u5e76\u6e05\u7406\u751f\u6210\u7684\u4e34\u65f6 <code>main<\/code>\u5305\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step3 \u6539\u8fdb\u5355\u5143\u6d4b\u8bd5<\/h4>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ec5\u4ec5\u5f15\u5165\u4e86\u4e00\u4e2a <code>testing<\/code> \u5305\u3002\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u63a5\u4e0b\u6765\u7684\u6d4b\u8bd5\u4e2d\u8fd8\u9700\u8981\u5176\u4ed6\u7684\u5305\uff0c\u6765\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u7684\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ math_test.go\uff08v2\uff09\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801<br>package math<br>\u200b<br>import (<br>  log \"github.com\/sirupsen\/logrus\"<br>  \"github.com\/stretchr\/testify\/require\"<br>  \"os\"<br>  \"testing\"<br>)<br>\u200b<br>func TestMain(t *testing.M) {<br>  log.Info(\"start unit test\")<br>  exitValue := t.Run()<br>  log.Info(\"end unit test\")<br>  os.Exit(exitValue)<br>}<br>\u200b<br>func TestAdd(t *testing.T) {<br>  require.Equal(t, 3, Add(1, 2))<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO[0000] start unit test &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>=== RUN &nbsp; TestAdd<br>--- PASS: TestAdd (0.00s)<br>PASS<br>INFO[0000] end unit test &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>ok &nbsp; &nbsp;  demo-unit-test\/math &nbsp; &nbsp; 0.566s<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4e0e <code>math_test.go (v1)<\/code> \u7684\u4e3b\u8981\u533a\u522b\u662f\u591a\u4e86\u4e00\u4e2a <code>TestMain(t *testing.M)<\/code> \u51fd\u6570\u548c\u5f15\u5165\u4e86 <code>testify<\/code> \u5305\u3002<code>TestMain<\/code> \u51fd\u6570\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a <code>package<\/code> \u4e2d\u6240\u6709\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u7684\u5165\u53e3\uff0c\u5b83\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u901a\u8fc7\u5728<code>TestMain<\/code>\u51fd\u6570\u4e2d\u8c03\u7528 <code>t.Run()<\/code> \u6765\u6267\u884c\u5f53\u524d <code>package<\/code> \u4e2d\u7684\u6240\u6709\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u4e2d\u7684\u6d4b\u8bd5\u51fd\u6570\u3002\u7531\u4e8e\u5728\u8c03\u7528 <code>t.Run()<\/code>\u4e4b\u524d\u5c31\u53ef\u4ee5\u8fd0\u884c\u4efb\u610f\u4ee3\u7801\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u5728\u6d4b\u8bd5\u51fd\u6570\u6267\u884c\u4e4b\u524d\u8bbe\u7f6e\u4e00\u4e9b\u9700\u8981\u7528\u5230\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u4e14\u76f8\u5bf9\u4e8e <code>init()<\/code> \u65b9\u6cd5\u6765\u8bf4\u5b83\u8fd8\u53ef\u4ee5\u5728\u8fd0\u884c\u5b8c\u5355\u5143\u6d4b\u8bd5\u51fd\u6570\u4e4b\u540e\u8fd0\u884c\u4efb\u610f\u4ee3\u7801\uff08\u6267\u884c\u4e00\u4e9b\u8d44\u6e90\u91ca\u653e\u76f8\u5173\u5de5\u4f5c\u7b49\uff09\u3002\u4f46\u5728\u4f7f\u7528 <code>TestMain<\/code> \u51fd\u6570\u65f6\uff0c\u9700\u8981\u8bb0\u4f4f\u4e0b\u9762\u4e24\u70b9\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7531\u4e8e\u51fd\u6570\u540d\u5728\u6307\u5b9a\u5305\u5185\u9700\u8981\u552f\u4e00\uff0c\u6240\u4ee5 <code>TestMain<\/code> \u51fd\u6570\u5728\u4e00\u4e2a\u5305\u5185\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002\u5982\u679c\u5305\u5185\u6709\u591a\u4e2a\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\uff0c\u5c31\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u628a <code>TestMain<\/code> \u51fd\u6570\u653e\u5728\u6700\u5408\u9002\u7684\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\u4e2d\u3002<\/li><li>\u5e76\u4e0d\u662f\u6240\u6709\u7684\u5355\u5143\u6d4b\u8bd5\u90fd\u9700\u8981 <code>TestMain<\/code> \u65b9\u6cd5\u3002<\/li><\/ul>\n\n\n\n<p>\u76f8\u5bf9\u4e8e<code>math_test.go (v1)<\/code> \u7684\u5355\u5143\u6d4b\u8bd5\uff0c <code>testify<\/code> \u5305\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u65b9\u4fbf\u7684\u5bf9\u6d4b\u8bd5\u7ed3\u679c\u8fdb\u884c\u65ad\u8a00\u3002<a href=\"https:\/\/github.com\/stretchr\/testify\">\u70b9\u51fb\u67e5\u770b\u5173\u4e8e\u66f4\u591a testify\u7684\u4ecb\u7ecd<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step4 \u67e5\u770b\u5355\u6d4b\u8986\u76d6\u7387<\/h4>\n\n\n\n<p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u53ca\u65f6\u4e86\u89e3\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u7684\u8986\u76d6\u7387\u4e5f\u5341\u5206\u91cd\u8981\u3002<code>go test<\/code> \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u5355\u5143\u6d4b\u8bd5\u7684\u8986\u76d6\u7387\uff0c\u5e76\u4e14\u914d\u5408 <code>go tool<\/code> \u547d\u4ee4\u80fd\u67e5\u770b\u66f4\u4e3a\u8be6\u7ec6\u7684\u8986\u76d6\u8303\u56f4\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u8ba1\u7b97\u5f53\u524d\u5355\u5143\u6d4b\u8bd5\u7684\u8986\u76d6\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">go test -v -cover<\/pre>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e0a\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INFO[0000] start unit test &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>=== RUN &nbsp; TestAdd<br>--- PASS: TestAdd (0.00s)<br>PASS<br>coverage: 50.0% of statements<br>INFO[0000] end unit test &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>ok &nbsp; &nbsp;  demo-unit-test\/math &nbsp; &nbsp; 0.514s<\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c\u8986\u76d6\u7387 <code>coverage<\/code> \u7684\u503c\u4e3a <code>50%<\/code> \u7684\u539f\u56e0\u662f\u5728\u8fd9\u4e2a <code>package<\/code> \u4e0b <code>math.go<\/code> \u4e00\u5171\u6709\u4e24\u4e2a\u51fd\u6570 <code>Add<\/code> \u548c <code>Sub<\/code> \uff0c\u4f46\u662f\u53ea\u4e3a <code>Add<\/code> \u7f16\u5199\u4e86\u5355\u5143\u6d4b\u8bd5\uff0c\u6240\u4ee5\u5355\u6d4b\u8986\u76d6\u7387\u4e3a <code>50%<\/code>\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u4e2a\u503c\u7684\u8ba1\u7b97\u65b9\u5f0f\u4e0d\u662f\u6839\u636e <code>TestAdd<\/code> \u7b7e\u540d\u4f5c\u4e3a\u4f9d\u636e\u7684\uff0c\u5982\u679c\u4f60\u628a <code>TestAdd<\/code> \u6539\u4e3a <code>TestAddd<\/code> \u540e\u5bf9\u8986\u76d6\u7387\u7684\u503c\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002\u4f46\u662f\u5728 <code>TestAdd<\/code> \u51fd\u6570\u4e2d\u589e\u52a0\u4e00\u884c\u4ee3\u7801 <code>require.Equal(t, -1, Sub(1, 2))<\/code> \u540e\u518d\u6267\u884c <code>go test -v -cover<\/code> \u8fd9\u884c\u547d\u4ee4\uff0c\u8fd9\u65f6\u4f60\u5c31\u4f1a\u53d1\u73b0\u8986\u76d6\u7387\u53d8\u6210\u4e86 <code>100%<\/code> \u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u6253\u5370\u8986\u76d6\u7387\u7684\u57fa\u7840\u4e0a\u751f\u6210\u4e00\u4efd\u66f4\u4e3a\u8be6\u7ec6\u7684\u8986\u76d6\u8303\u56f4\u62a5\u544a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">go test -v -coverprofile=coverage.out<\/pre>\n\n\n\n<p>\u8fd0\u884c\u4e0b\u9762\u547d\u4ee4\uff0c\u67e5\u770b\u6bcf\u4e2a\u51fd\u6570\u7684\u5355\u6d4b\u8986\u76d6\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">go tool cover -func coverage.out<\/pre>\n\n\n\n<p>\u63a7\u5236\u53f0\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">demo-unit-test\/math\/math.go:3:  Add &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100.0%<br>demo-unit-test\/math\/math.go:7:  Sub &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0%<br>total: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  (statements) &nbsp; &nbsp;50.0%<\/pre>\n\n\n\n<p>\u5982\u679c\u6211\u628a <code>Add<\/code> \u51fd\u6570\u6539\u6210\u4e0b\u9762\u8fd9\u79cd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func Add(a, b int) int {<br>  if a &lt; 0 {<br>    return 0<br>  }<br>  return a + b<br>}<\/pre>\n\n\n\n<p>\u518d\u6b21\u6267\u884c <code>go tool cover -func coverage.out<\/code> \u547d\u4ee4\u7684\u8f93\u51fa\u5219\u4f1a\u53d8\u6210\u4e0b\u9762\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">demo-unit-test\/math\/math.go:3:  Add &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 66.7%<br>demo-unit-test\/math\/math.go:10: Sub &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0%<br>total: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  (statements) &nbsp; &nbsp;50.0%<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230 <code>Add<\/code> \u51fd\u6570\u7684\u5355\u5143\u6d4b\u8bd5\u8986\u76d6\u7387\u53d8\u6210\u4e86 <code>66.7%<\/code>\u3002\u6240\u4ee5\uff0c\u5728\u6211\u4eec\u5199\u7684\u5355\u6d4b\u8981\u5c3d\u53ef\u80fd\u7684\u628a\u6240\u6709\u7684\u903b\u8f91\u5206\u652f\u90fd\u6267\u884c\u5230\uff0c\u5305\u62ec\u5f02\u5e38\u60c5\u51b5\u3002\u9488\u5bf9\u4e00\u4e2a\u88ab\u6d4b\u8bd5\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u80fd\u5c31\u8981\u6784\u9020\u51e0\u79cd\u4e0d\u540c\u7684\u8f93\u5165\u6765\u4f7f\u8986\u76d6\u7387\u8fbe\u5230\u4e00\u4e2a\u66f4\u9ad8\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u4e0b\u9762\u547d\u4ee4\uff0c\u5728 Web \u6d4f\u89c8\u5668\u6253\u5f00 <code>coverage.out<\/code> \u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">go tool cover -html=coverage.out<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"333\" src=\"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png\" alt=\"\" class=\"wp-image-1667\" srcset=\"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png 602w, https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1-300x166.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u7ed9\u4e1a\u52a1\u4ee3\u7801\u5199\u5355\u6d4b\uff1f<\/h3>\n\n\n\n<p>\u5728\u5b9e\u9645\u7684\u9879\u76ee\u4e2d\uff0c\u4e0d\u4ec5\u6709\u7b80\u5355\u7684 CURD \u7c7b\u7684\u51fd\u6570\u4e5f\u4f1a\u6709\u4e00\u4e9b\u8c03\u7528\u94fe\u8f83\u957f\u6216\u8005\u4f1a\u4f9d\u8d56\u5176\u4ed6\u670d\u52a1\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u524d\u8005\u6765\u8bf4\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u6b64\u7c7b\u4ee3\u7801\u6587\u4ef6\u653e\u7f6e\u5728\u540c\u4e00\u4e2a <code>package<\/code> \u4e2d\u3002\u5728\u5176\u4e2d\u7684\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u7684 <code>TestMain<\/code> \u51fd\u6570\u4e2d\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u6b64 <code>package<\/code> \u4e0b\u6240\u6709\u7684\u5355\u5143\u6d4b\u8bd5\u90fd\u590d\u7528\u8fd9\u4e00\u4e2a\u8fde\u63a5\uff0c\u6267\u884c\u5b8c <code>package<\/code> \u4e2d\u6240\u6709\u7684\u5355\u6d4b\u4e4b\u540e\u518d\u91ca\u653e\u6389\u5373\u53ef\u3002\u5728\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u4e2d\u53ef\u80fd\u4f1a\u5b58\u5728 <code>TestCreate<\/code> \u548c <code>TestDelete<\/code> \u4e24\u4e2a\u51fd\u6570\uff0c\u6709\u540c\u5b66\u53ef\u80fd\u4f1a\u95ee\u5728 <code>TestDelete<\/code> \u4e2d\u80fd\u5426\u4f7f\u7528 <code>TestCreate<\/code> \u51fd\u6570\u4e2d\u521b\u5efa\u7684\u6570\u636e\uff1f\u5982\u679c\u5728\u4e1a\u52a1\u4e0a <code>Add<\/code> \u51fd\u6570\u548c <code>Delete<\/code> \u51fd\u6570\u662f\u5f3a\u5173\u8054\u7684\uff0c\u90a3\u4e48\u540e\u9762\u7684\u5220\u9664\u51fd\u6570\u4e5f\u662f\u53ef\u4ee5\u4f7f\u7528\u524d\u9762\u521b\u5efa\u7684\u6d4b\u8bd5\u7528\u4f8b\u6570\u636e\u7684\u3002\u5f53\u7136\uff0c\u5982\u679c\u4e3a\u4e86\u4fdd\u6301\u6bcf\u4e2a\u5355\u5143\u6d4b\u8bd5\u51fd\u6570\u7684\u72ec\u7acb\u6027\u4f60\u4e5f\u53ef\u4ee5\u5728\u6bcf\u4e2a\u51fd\u6570\u4e2d\u6784\u9020\u5f53\u524d\u51fd\u6570\u6240\u9700\u8981\u7684\u6570\u636e\u3002\u5173\u4e8e\u5355\u6d4b\u6570\u636e\u7684\u51c6\u5907\u548c\u5904\u7406\uff0c\u5e94\u8be5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u7075\u6d3b\u7684\u9009\u62e9\u9002\u5408\u7684\u65b9\u6848\uff0c\u4e0d\u8981\u62d8\u6ce5\u4e8e\u5f62\u5f0f\uff0c\u201c\u4e0d\u7ba1\u767d\u732b\u9ed1\u732b\uff0c\u6293\u4f4f\u8001\u9f20\u5c31\u662f\u597d\u732b\u201d\u3002<\/p>\n\n\n\n<p>\u9488\u5bf9\u4f9d\u8d56\u5176\u4ed6\u670d\u52a1\u7684\u51fd\u6570\uff0c\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u63d0\u4f9b\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u670d\u52a1\u914d\u7f6e\u5230\u9879\u76ee\u4e2d\u4f9b\u5355\u6d4b\u51fd\u6570\u4f7f\u7528\u3002\u53e6\u5916\u4e00\u79cd\u5c31\u662f\u5c06\u7b2c\u4e09\u65b9\u670d\u52a1\u62bd\u8c61\u6210\u63a5\u53e3\uff0c\u5728\u88ab\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u8c03\u7528\u63a5\u53e3\u7684\u65b9\u6cd5\uff0c\u5728\u6d4b\u8bd5\u65f6\u4f20\u5165 Mock \u7c7b\u578b\uff0c\u4ece\u800c\u5c06\u4f9d\u8d56\u7684\u670d\u52a1\u4ece\u5177\u4f53\u7684\u88ab\u6d4b\u8bd5\u51fd\u6570\u4e2d\u89e3\u8026\u51fa\u53bb\u3002\u4e3a\u4e86\u5e2e\u52a9\u4f60\u7406\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u4ee5\u4e00\u6bb5\u4ee3\u7801\u4e3a\u4f8b\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package face<br> &nbsp; &nbsp;<br>import (<br> &nbsp;\"context\"<br> &nbsp;\"google.golang.org\/grpc\"<br>) <br>\u200b<br>type DetectReq struct {<br> &nbsp; &nbsp;Id &nbsp; string `json:\"id\"`<br> &nbsp; &nbsp;URI &nbsp;string `json:\"uri\"`<br> &nbsp; &nbsp;Type string `json:\"type\"`<br>}<br>\u200b<br>type DetectRes struct {<br> &nbsp; &nbsp;Id &nbsp; &nbsp; &nbsp;string `json:\"id\"`<br> &nbsp; &nbsp;Feature string `json:\"feature\"`<br>}<br>\u200b<br>func Detect(req *DetectReq, ctx context.Context, client *grpc.ClientConn) (*DetectRes, error) {<br> &nbsp;\/\/ other<br> &nbsp;return clinet.Detect(req, ctx)<br>}<\/pre>\n\n\n\n<p><code>Detect<\/code> \u51fd\u6570\u4f9d\u8d56\u4e8e\u4e00\u4e2a gRPC \u8fde\u63a5\uff0c\u4f46\u662f\u5f53\u6211\u4eec\u5728\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u65f6\u53ef\u80fd\u7531\u4e8e\u79cd\u79cd\u539f\u56e0\u65e0\u6cd5\u65e0\u6cd5\u5efa\u7acb gRPC \u8fde\u63a5\uff0c\u4ece\u800c\u5f71\u54cd\u5355\u5143\u6d4b\u8bd5\u7684\u6267\u884c\u3002\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u8fd9\u79cd\u6539\u52a8\uff0c\u8ba9\u5b83\u8fd0\u884c\u8d77\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package face<br> &nbsp; &nbsp;<br>import (<br> &nbsp;\"context\"<br> &nbsp;\"google.golang.org\/grpc\"<br>) <br>\u200b<br>type DetectReq struct {<br> &nbsp; &nbsp;Id &nbsp; string `json:\"id\"`<br> &nbsp; &nbsp;URI &nbsp;string `json:\"uri\"`<br> &nbsp; &nbsp;Type string `json:\"type\"`<br>}<br>\u200b<br>type DetectRes struct {<br> &nbsp; &nbsp;Id &nbsp; &nbsp; &nbsp;string `json:\"id\"`<br> &nbsp; &nbsp;Feature string `json:\"feature\"`<br>}<br>\u200b<br>type EngineService interface {<br> &nbsp;Detect(req *DetectReq, ctx context.Context) (*DetectRes, error)<br>}<br>\u200b<br>func Detect(req *DetectReq, ctx context.Context, svc *EngineService) (*DetectRes, error) {<br> &nbsp;\/\/ other<br> &nbsp;return svc.Detect(req, ctx)<br>}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06 <code>Detect<\/code> \u51fd\u6570\u4e0e gRPC \u89e3\u8026\uff0c\u53ea\u8981\u6211\u4eec\u4f20\u5165\u4e00\u4e2a\u5b9e\u73b0\u4e86 <code>EngineService<\/code> \u63a5\u53e3\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c<code>Detect<\/code> \u51fd\u6570\u4fbf\u53ef\u6210\u529f\u8fd0\u884c\u3002\u8fd9\u6837\u6211\u4eec\u5728\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u5c31\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u4e0d\u4f9d\u8d56\u5176\u4ed6\u670d\u52a1\u7684\u5b9e\u4f8b\u4f20\u5165 <code>Detect<\/code> \u51fd\u6570\u5373\u53ef\u3002\u9488\u5bf9\u6539\u5199\u540e\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e0b\u9762\u8fd9\u79cd\u5355\u5143\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package face<br>\u200b<br>import (<br>  log \"github.com\/sirupsen\/logrus\"<br>  \"github.com\/stretchr\/testify\/require\"<br>  \"os\"<br>  \"testing\"<br>)<br>\u200b<br>type faceEngineService struct {}<br>\u200b<br>type NewFakeEngineService() *EngineService {<br> &nbsp; &nbsp;return &amp;faceEngineService{}<br>}<br>\u200b<br>func (e *faceEngineService) Detect(req *DetectReq, ctx context.Context) (*DetectRes, error) {<br> &nbsp; &nbsp;res := &amp;DetectRes{<br> &nbsp; &nbsp; &nbsp;Id: &nbsp; &nbsp; &nbsp;\"2dc64674-1bac-4d6d-b4c7-54ab6b66dd91\",<br> &nbsp; &nbsp; &nbsp;Feature: \"HDGBHKJHEKJBFHJKBHBCVEHVGCVGEVFGE\",<br> &nbsp;  }<br> &nbsp; &nbsp;return res, nil<br>}<br>\u200b<br>func TestDetect(t *testing.T) {<br> &nbsp; &nbsp;ctx, _ := context.WithTimeout(s.ctx, time.Minute*30)<br> &nbsp; &nbsp;fake := NewFakeEngineService()<br> &nbsp; &nbsp;req := &amp;DetectReq{<br> &nbsp; &nbsp; &nbsp; Id: \"2dc64674-1bac-4d6d-b4c7-54ab6b66dd91\",<br> &nbsp; &nbsp; &nbsp; URI: \"https:\/\/www.example.com\/xxx.jpg\",<br> &nbsp; &nbsp; &nbsp; Type: \"face\",<br> &nbsp;  }<br> &nbsp; &nbsp;res, err := Detect(req, ctx, fake)<br> &nbsp; &nbsp;require.NoError(t, err)<br> &nbsp; &nbsp;require.NotNil(t, res)<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u6765\u8bf4\uff0c\u5176\u5b9e\u8fd8\u6709\u5404\u79cd\u5404\u6837\u7684\u5e93\u548c\u5de5\u5177\u53ef\u4f9b\u5f00\u53d1\u8005\u4f7f\u7528\u3002\u4f8b\u5982\u4f7f\u7528 <code>gotests<\/code> \u5de5\u5177\u81ea\u52a8\u751f\u6210\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\uff0c\u51cf\u5c11\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u91cf\u3002<code>golang\/mock<\/code> \u5b9e\u73b0\u4e86\u57fa\u4e8e interface \u7684 Mock \u529f\u80fd\uff0c\u80fd\u591f\u4e0e Golang \u5185\u7f6e\u7684 <code>testing<\/code> \u5305\u8fdb\u884c\u5f88\u597d\u7684\u7ee7\u627f\u3002<code>sqlmock<\/code> \u53ef\u4ee5\u7528\u6765\u6a21\u62df\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5047\u5982\u4f60\u5728\u5355\u6d4b\u65f6\u6ca1\u6709\u53ef\u7528\u7684\u6570\u636e\u5e93\u5c31\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5b83\u3002\u8fd8\u6709\u4e00\u4e2a\u7ec8\u6781\u89e3\u51b3\u65b9\u6848\u5305 <code>bouk\/monkey<\/code> \u80fd\u591f\u901a\u8fc7\u66ff\u6362\u51fd\u6570\u6307\u9488\u7684\u65b9\u5f0f\u6765\u4fee\u6539\u4efb\u610f\u51fd\u6570\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u5230\u5e95\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u6765\u89e3\u51b3\u4f60\u7684\u5355\u5143\u6d4b\u8bd5\u4f9d\u8d56\u95ee\u9898\uff0c\u4ece\u800c\u4f7f\u5355\u5143\u6d4b\u8bd5\u8986\u76d6\u7387\u66f4\u9ad8\uff0c\u5e76\u6ca1\u6709\u4e00\u4e2a\u6807\u51c6\u7684\u7b54\u6848\uff0c\u9002\u5408\u7684\u5c31\u662f\u6700\u597d\u7684\u3002\u4e0d\u8fc7\u5ea6\u8ffd\u6c42\u590d\u6742\u7684\u8bbe\u8ba1\uff0c\u4e0d\u59a5\u534f\u7cdf\u7cd5\u7684\u4ee3\u7801\u8d28\u91cf\uff0c\u5728\u6b64\u4e4b\u95f4\u5bfb\u627e\u4e00\u4e2a\u4f60\u8ba4\u4e3a\u5e73\u8861\u7684\u70b9\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u6bb5\u5728\u5e94\u7528\u7a0b\u5e8f\u6216\u5305\u4e2d\u7528\u4e8e\u6d4b\u8bd5\u7a0b\u5e8f\u529f\u80fd\u662f\u5426\u6b63\u786e\u7684\u51fd\u6570\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6709\u7740\u5341\u5206\u91cd\u8981\u7684\u4f5c\u7528\u3002 \u4ece\u4e00 &hellip; <a href=\"https:\/\/www.changxuan.top\/?p=1666\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[105],"class_list":["post-1666","post","type-post","status-publish","format-standard","hentry","category-tech","tag-golang"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5 - \u5e38\u8f69<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.changxuan.top\/?p=1666\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5 - \u5e38\u8f69\" \/>\n<meta property=\"og:description\" content=\"\u524d\u8a00 \u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u6bb5\u5728\u5e94\u7528\u7a0b\u5e8f\u6216\u5305\u4e2d\u7528\u4e8e\u6d4b\u8bd5\u7a0b\u5e8f\u529f\u80fd\u662f\u5426\u6b63\u786e\u7684\u51fd\u6570\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6709\u7740\u5341\u5206\u91cd\u8981\u7684\u4f5c\u7528\u3002 \u4ece\u4e00 &hellip; \u7ee7\u7eed\u9605\u8bfbGolang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.changxuan.top\/?p=1666\" \/>\n<meta property=\"og:site_name\" content=\"\u5e38\u8f69\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-18T15:34:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-19T02:59:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png\" \/>\n<meta name=\"author\" content=\"\u5e38\u8f69\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u5e38\u8f69\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666\"},\"author\":{\"name\":\"\u5e38\u8f69\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"headline\":\"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5\",\"datePublished\":\"2022-06-18T15:34:58+00:00\",\"dateModified\":\"2022-06-19T02:59:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666\"},\"wordCount\":19,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"image\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/unit-test-ex-1.png\",\"keywords\":[\"Golang\"],\"articleSection\":[\"\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666\",\"name\":\"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5 - \u5e38\u8f69\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/unit-test-ex-1.png\",\"datePublished\":\"2022-06-18T15:34:58+00:00\",\"dateModified\":\"2022-06-19T02:59:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.changxuan.top\\\/?p=1666\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#primaryimage\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/unit-test-ex-1.png\",\"contentUrl\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/unit-test-ex-1.png\",\"width\":602,\"height\":333},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1666#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/www.changxuan.top\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#website\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/\",\"name\":\"\u5e38\u8f69\",\"description\":\"\u95fb\u9053\u6709\u5148\u540e\uff0c\u672f\u4e1a\u6709\u4e13\u653b-\u4e00\u4e8c\u4e09\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.changxuan.top\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\",\"name\":\"\u5e38\u8f69\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"contentUrl\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"width\":960,\"height\":960,\"caption\":\"\u5e38\u8f69\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\"},\"description\":\"\u603b\u8981\u505a\u70b9\u4ec0\u4e48\u5427\uff01\",\"sameAs\":[\"https:\\\/\\\/www.changxuan.top\"],\"url\":\"https:\\\/\\\/www.changxuan.top\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5 - \u5e38\u8f69","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.changxuan.top\/?p=1666","og_locale":"zh_CN","og_type":"article","og_title":"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5 - \u5e38\u8f69","og_description":"\u524d\u8a00 \u5355\u5143\u6d4b\u8bd5\u662f\u4e00\u6bb5\u5728\u5e94\u7528\u7a0b\u5e8f\u6216\u5305\u4e2d\u7528\u4e8e\u6d4b\u8bd5\u7a0b\u5e8f\u529f\u80fd\u662f\u5426\u6b63\u786e\u7684\u51fd\u6570\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6709\u7740\u5341\u5206\u91cd\u8981\u7684\u4f5c\u7528\u3002 \u4ece\u4e00 &hellip; \u7ee7\u7eed\u9605\u8bfbGolang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5","og_url":"https:\/\/www.changxuan.top\/?p=1666","og_site_name":"\u5e38\u8f69","article_published_time":"2022-06-18T15:34:58+00:00","article_modified_time":"2022-06-19T02:59:18+00:00","og_image":[{"url":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png","type":"","width":"","height":""}],"author":"\u5e38\u8f69","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u5e38\u8f69","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.changxuan.top\/?p=1666#article","isPartOf":{"@id":"https:\/\/www.changxuan.top\/?p=1666"},"author":{"name":"\u5e38\u8f69","@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"headline":"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5","datePublished":"2022-06-18T15:34:58+00:00","dateModified":"2022-06-19T02:59:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.changxuan.top\/?p=1666"},"wordCount":19,"commentCount":0,"publisher":{"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"image":{"@id":"https:\/\/www.changxuan.top\/?p=1666#primaryimage"},"thumbnailUrl":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png","keywords":["Golang"],"articleSection":["\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.changxuan.top\/?p=1666#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.changxuan.top\/?p=1666","url":"https:\/\/www.changxuan.top\/?p=1666","name":"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5 - \u5e38\u8f69","isPartOf":{"@id":"https:\/\/www.changxuan.top\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.changxuan.top\/?p=1666#primaryimage"},"image":{"@id":"https:\/\/www.changxuan.top\/?p=1666#primaryimage"},"thumbnailUrl":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png","datePublished":"2022-06-18T15:34:58+00:00","dateModified":"2022-06-19T02:59:18+00:00","breadcrumb":{"@id":"https:\/\/www.changxuan.top\/?p=1666#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.changxuan.top\/?p=1666"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.changxuan.top\/?p=1666#primaryimage","url":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png","contentUrl":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2022\/06\/unit-test-ex-1.png","width":602,"height":333},{"@type":"BreadcrumbList","@id":"https:\/\/www.changxuan.top\/?p=1666#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.changxuan.top\/"},{"@type":"ListItem","position":2,"name":"Golang \u5355\u5143\u6d4b\u8bd5\u5b9e\u8df5"}]},{"@type":"WebSite","@id":"https:\/\/www.changxuan.top\/#website","url":"https:\/\/www.changxuan.top\/","name":"\u5e38\u8f69","description":"\u95fb\u9053\u6709\u5148\u540e\uff0c\u672f\u4e1a\u6709\u4e13\u653b-\u4e00\u4e8c\u4e09","publisher":{"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.changxuan.top\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd","name":"\u5e38\u8f69","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","url":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","contentUrl":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","width":960,"height":960,"caption":"\u5e38\u8f69"},"logo":{"@id":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg"},"description":"\u603b\u8981\u505a\u70b9\u4ec0\u4e48\u5427\uff01","sameAs":["https:\/\/www.changxuan.top"],"url":"https:\/\/www.changxuan.top\/?author=1"}]}},"views":3294,"_links":{"self":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/1666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1666"}],"version-history":[{"count":3,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/1666\/revisions"}],"predecessor-version":[{"id":1671,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/1666\/revisions\/1671"}],"wp:attachment":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}