{"id":103421,"date":"2020-05-22T21:32:07","date_gmt":"2020-05-22T13:32:07","guid":{"rendered":"http:\/\/4563.org\/?p=103421"},"modified":"2020-05-22T21:32:07","modified_gmt":"2020-05-22T13:32:07","slug":"dig101-go-%e4%b9%8b%e5%a6%82%e4%bd%95%e5%9c%a8%e5%87%bd%e6%95%b0%e5%86%85%e4%bf%ae%e6%94%b9%e6%8c%87%e9%92%88","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=103421","title":{"rendered":"Dig101-Go \u4e4b\u5982\u4f55\u5728\u51fd\u6570\u5185\u4fee\u6539\u6307\u9488"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Dig101-Go \u4e4b\u5982\u4f55\u5728\u51fd\u6570\u5185\u4fee\u6539\u6307\u9488               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : newmiao <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<blockquote>\n<p>Dig101: dig more, simplified more and know more<\/p>\n<\/blockquote>\n<p>\u4eca\u5929\u6765\u770b\u4e00\u4e2a\u5c0f\u95ee\u9898\uff1a<strong>\u5982\u4f55\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u4e00\u4e2a\u6307\u9488\uff08\u53c2\u6570\u6216\u63a5\u6536\u8005\uff09\uff0c\u4f7f\u5176\u503c\u7684\u6539\u53d8\u80fd\u53cd\u6620\u5728\u51fd\u6570\u5916\u90e8<\/strong>\uff1f<\/p>\n<p>\u76f4\u63a5\u4e0a\u4ee3\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u4e48\uff1f<\/p>\n<pre><code>type ArgType struct {   A string   b int }  func modifyPointerArg1(arg *ArgType) {   arg = &amp;ArgType{\"arg1\", 1}   fmt.Println(\"inside modifyPointerArg1:\", arg) } <\/code><\/pre>\n<p>\u7b54\u6848\u662f [\u4e0d\u53ef\u4ee5]<\/p>\n<p>\u7b49\u4f1a\u5206\u6790\uff0c\u518d\u770b\u4e00\u4e2a\uff0c\u8fd9\u4e2a\u5462\uff1f<\/p>\n<pre><code>func modifyPointerArg2(arg *ArgType) {   *arg = ArgType{\"arg2\", 2}   fmt.Println(\"inside modifyPointerArg2:\", arg) } <\/code><\/pre>\n<p>\u7b54\u6848\u662f [\u53ef\u4ee5]<\/p>\n<p>\u4ed4\u7ec6\u770b\u4e0b\uff0c\u4f60\u5e94\u8be5\u5c31\u660e\u767d\u4e86\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u66ff\u6362\u7684\u662f<strong>\u6307\u9488\u53d8\u91cf\u672c\u8eab<\/strong>,<\/p>\n<p>\u4e5f\u5c31\u662f\u5728\u51fd\u6570<code>modifyPointerArg1<\/code>\u7684\u4f5c\u7528\u57df\u5185\uff0c\u5176\u4fee\u6539\u662f\u6709\u6548<\/p>\n<p>\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u5e76\u4e0d\u5f71\u54cd\u6307\u9488<code>arg<\/code>\u6240\u6307\u5411\u7684\u503c\uff08\u522b\u5fd8\u4e86\uff0cGo \u53c2\u6570\u4f20\u9012\u662f\u503c\u4f20\u9012\u561b\uff01\uff09<\/p>\n<p>\u81f3\u4e8e<code>modifyPointerArg2<\/code>\u5219\u662f\u5bf9\u6307\u9488\u89e3\u5f15\u7528\uff0c\u4fee\u6539\u4e86<strong>\u5176\u6307\u5411\u7684\u503c<\/strong><\/p>\n<p>\u8fd9\u6837\u7684\u65b9\u5f0f\u5176\u5b9e\u8fd8\u6709\u5f88\u591a\uff0c\u6bd4\u5982\u8fd9\u4e2a\uff1a<\/p>\n<pre><code>func modifyPointerArg3(arg *ArgType) {   val := reflect.ValueOf(arg)   val.Elem().FieldByName(\"A\").SetString(\"arg3\")   fmt.Println(\"inside modifyPointerArg3:\", arg)   \/\/ val.Elem().FieldByName(\"b\").SetInt(3)   \/\/ panic: reflect: reflect.flag.mustBeAssignable using value obtained using unexported field } <\/code><\/pre>\n<p>\u5b9e\u9645\u662f\u5229\u7528\u53cd\u5c04\u7684<code>Elem()<\/code>\u83b7\u53d6<code>val<\/code>\u7684\u503c<\/p>\n<ul>\n<li>\u5982\u679c\u5176\u4e3a\u7a7a\u63a5\u53e3\uff08<code>empty interface<\/code>\uff09\uff0c\u5219\u83b7\u53d6\u5176\u5185\u90e8\u503c\uff08\u7a7a\u63a5\u53e3\u503c\u5b57\u6bb5\u7684\u7c7b\u578b\u662f\u6307\u9488\u54e6\uff09<\/li>\n<li>\u5982\u679c\u5176\u4e3a\u6307\u9488\uff08<code>pointer<\/code>\uff09\uff0c\u5219\u83b7\u53d6\u5176\u6307\u5411\u7684\u503c<\/li>\n<\/ul>\n<p>\u83b7\u53d6\u5230\u7684\u7ed3\u6784\u5982\u4e0b<\/p>\n<pre><code>type Value struct {     \/\/ \u7c7b\u578b     typ *rtype     \/\/ \u503c\u6307\u9488     ptr unsafe.Pointer     \/\/ \u6807\u5fd7\u4f4d   flag } <\/code><\/pre>\n<p>\u7136\u540e\u5bf9\u5e94\u7c7b\u578b\u4fee\u6539\u65f6, \u5b9e\u9645\u5c31\u662f\u5bf9\u6307\u9488\u89e3\u5f15\u7528\u4fee\u6539\u5176\u6307\u5411\u7684\u503c<\/p>\n<pre><code>func (v Value) SetString(x string) {   v.mustBeAssignable()   v.mustBe(String)   \/\/ \u8fd9\u91cc   *(*string)(v.ptr) = x } <\/code><\/pre>\n<blockquote>\n<p>Tips\uff1a \u8fd9\u91cc\u6ce8\u610f\u4e00\u70b9\uff0c\u4e0d\u5bfc\u51fa\u7684\u5b57\u6bb5\uff08\u7ed3\u6784\u4f53\u5185\u5c0f\u5199\u7684\u5b57\u6bb5\uff09\u4e0d\u80fd\u7528\u6b64\u7c7b\u65b9\u6cd5\u4fee\u6539\uff0c\u4f1a panic \uff01<\/p>\n<\/blockquote>\n<p>\u518d\u5982\uff1a<\/p>\n<pre><code>func modifyPointerArg4(arg *ArgType) {   jsonStr := `{\"A\":\"arg4\",\"b\":4}`   json.Unmarshal([]byte(jsonStr), arg) } <\/code><\/pre>\n<p>\u5185\u90e8\u5b9e\u9645\u4e5f\u662f\u7528\u4e86\u53cd\u5c04\u4fee\u6539\u6307\u9488\u6307\u5411\u7684\u503c<\/p>\n<p>\u53e6\u5916\uff0c\u628a\u4e0a\u8fb9\u51e0\u4e2a\u6d4b\u8bd5\u51fd\u6570\u7531<strong>\u6307\u9488\u53c2\u6570<\/strong>\u6362\u4e3a<strong>\u6307\u9488\u63a5\u53d7\u8005<\/strong>\uff0c\u4e5f\u662f\u4e00\u6837\u7684<\/p>\n<p>\u6bd4\u5982\uff1a<\/p>\n<pre><code>func (arg *ArgType) modifyPointerReceiver4() {   jsonStr := `{\"A\":\"arg4\",\"b\":4}`   json.Unmarshal([]byte(jsonStr), arg) } <\/code><\/pre>\n<p>\u6709\u5174\u8da3\u53ef\u4ee5\u53bb\u81ea\u884c\u5c1d\u8bd5\u4e0b\u5176\u4ed6\u51e0\u4e2a\u51fd\u6570\u3002<\/p>\n<p>\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u8fd9\u6837\u4fee\u6539\u6709\u5565\u7528\u4e48\uff1f<\/p>\n<p>\u5e38\u89c1\u7684\u4e00\u4e2a\u573a\u666f\u4fbf\u662f\uff1a<\/p>\n<p><strong>\u6d4b\u8bd5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bf9\u63a5\u53e3\u5b9e\u73b0\u5bf9\u5e94\u7684 mock \u51fd\u6570\uff0c\u6539\u53d8\u53c2\u6570\u6216\u63a5\u6536\u8005\uff0c\u4ee5\u8fbe\u5230\u6392\u9664\u4f9d\u8d56\uff0c\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u76ee\u7684\u3002<\/strong><\/p>\n<p>\u6587\u7ae0\u9996\u53d1\u516c\u4f17\u53f7\uff1anewbmiao<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1aDig101-Go \u7cfb\u5217<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200526_5eccd6e6b819f.png\" alt=\"Dig101-Go \u4e4b\u5982\u4f55\u5728\u51fd\u6570\u5185\u4fee\u6539\u6307\u9488\" \/><\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Dig101-Go \u4e4b\u5982\u4f55\u5728\u51fd\u6570\u5185&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/103421"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103421"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/103421\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103421"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}