{"id":207332,"date":"2025-07-08T08:43:32","date_gmt":"2025-07-08T00:43:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207332\/"},"modified":"2025-07-08T08:43:32","modified_gmt":"2025-07-08T00:43:32","slug":"%e5%8c%85fmt%e8%bf%90%e8%a1%8c%e6%97%b6%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207332\/","title":{"rendered":"\u5305\u201cfmt\u201d\u8fd0\u884c\u65f6\u95ee\u9898"},"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>\u5305\u201cfmt\u201d\u8fd0\u884c\u65f6\u95ee\u9898<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 19:18: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\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5305\u201cfmt\u201d\u8fd0\u884c\u65f6\u95ee\u9898\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u9047\u5230\u4e86\u4e00\u4e2a\u770b\u4f3c\u7b80\u5355\u4f46\u65e0\u6cd5\u91cd\u73b0\u7684\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u65e0\u6cd5\u89e3\u91ca\u3002 <\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u53d1\u751f\u5728\u751f\u4ea7\u4e2d\uff0c\u795e\u79d8\u7684\u662f\u5b83\u5f88\u5c11\u53d1\u751f\uff08\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u65e0\u6cd5\u91cd\u73b0\u5b83\uff09\uff0c\u8fd9\u53ef\u80fd\u662f\u6211\u65e0\u6cd5\u4e3e\u4f8b\u8bf4\u660e\u7684\u4e00\u4e2a\u56e0\u7d20\uff0c\u4f46\u4e0a\u4e0b\u6587\u5982\u4e0b\uff1a<\/p>\n<pre>type mytype struct {\n    field1 string\n    field2 int\n    field3 time.time\n    field4 []float64\n    \/\/ no pointers fields\n}\n\nfunc main() {\n    var mychan = make(chan interface{})\n\n    go func() {\n\/\/ this routine is reading and parsing messages from a ws dial and writing it in a channel\n\/\/ there is 2 only possible answer in the channel : a string, or a \"mytype\" like (with more fields, but no pointers in)     \n        var object mytype\n        object.field1 = \"test\"\n        \/\/ ..\n\n        mychan &lt;- object\n    }()\n\n    go func() {\n\/\/ this routine is sending a message to a ws dial and wait for the answer in a channel fed by another routine :\n        var object interface{}\n        go func(get *interface{}) {\n            *get = &lt;- mychan\n        } (&amp;object)\n        for object == nil {\n            time.sleep(time.nanosecond * 1)\n        }\n        log.println(fmt.sprint(object)) \/\/ panic here from the fmt.sprint() func\n    }()\n}<\/pre>\n<p>\u7d27\u6025\u5806\u6808\u8ddf\u8e2a\uff1a<\/p>\n<pre>runtime error: invalid memory address or nil pointer dereference\npanic(0x87a840, 0xd0ff40)\n    X:\/Go\/GoRoot\/src\/runtime\/panic.go:522 +0x1b5\nreflect.Value.String(0x85a060, 0x0, 0xb8, 0xc00001e500, 0xc0006f1a80)\n    X:\/Go\/GoRoot\/src\/reflect\/value.go:1781 +0x45\nfmt.(*pp).printValue(0xc006410f00, 0x85a060, 0x0, 0xb8, 0x76, 0x1)\n    X:\/Go\/GoRoot\/src\/fmt\/print.go:747 +0x21c3\nfmt.(*pp).printValue(0xc006410f00, 0x8ed5c0, 0x0, 0x99, 0x76, 0x0)\n    X:\/Go\/GoRoot\/src\/fmt\/print.go:796 +0x1b52\nfmt.(*pp).printArg(0xc006410f00, 0x8ed5c0, 0x0, 0x76)\n    X:\/Go\/GoRoot\/src\/fmt\/print.go:702 +0x2ba\nfmt.(*pp).doPrint(0xc006410f00, 0xc0006f22a0, 0x1, 0x1)\n    X:\/Go\/GoRoot\/src\/fmt\/print.go:1147 +0xfd\nfmt.Sprint(0xc0006f22a0, 0x1, 0x1, 0x0, 0x0)\n    X:\/Go\/GoRoot\/src\/fmt\/print.go:250 +0x52<\/pre>\n<p><strong>go \u7248\u672c\uff1a1.12.1 windows\/amd64<\/strong><\/p>\n<p>\u611f\u8c22\u60a8\u7684\u5b9d\u8d35\u65f6\u95f4\uff0c\u6211\u5e0c\u671b\u6709\u4eba\u80fd\u5411\u6211\u89e3\u91ca\u4e00\u4e0b\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u573a\u6570\u636e\u7ade\u8d5b\uff1a<\/p>\n<pre>var object interface{}\n    go func(get *interface{}) {\n        *get = &lt;- mychan\n    } (&amp;object)\n    for object == nil {\n        time.sleep(time.nanosecond * 1)\n    }<\/pre>\n<p>\u5199\u5165\u53d8\u91cf <code>object<\/code> \u7684 goroutine \u4e0d\u4f7f\u7528\u9501\uff1a\u5b83\u4ece\u901a\u9053\u63a5\u6536\u6570\u636e\uff0c\u5f53\u5b83\u83b7\u5f97\u4e00\u4e2a\u503c\u65f6\uff0c\u5c06\u5176\u5199\u5165 <code>*get<\/code>\uff0c\u5176\u4e2d <code>get == &amp;object<\/code>\uff0c\u4ee5\u4fbf\u5199\u5165\u63a5\u53e3<code>object<\/code> \u7684\u503c\u3002<\/p>\n<p>\u540c\u65f6\uff0c\u8fd0\u884c <code>for<\/code> \u5faa\u73af\u7684 goroutine \u4ece <code>object<\/code> \u8bfb\u53d6\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u96f6\u3002\u5b83\u5728\u4e0d\u4f7f\u7528\u9501\u7684\u60c5\u51b5\u4e0b\u8bfb\u53d6\uff0c\u56e0\u6b64\u53ef\u4ee5\u8bfb\u53d6\u90e8\u5206\u5199\u5165\u7684\u503c\u3002<\/p>\n<p>\u5b9e\u9645\u53d1\u751f\u7684\u60c5\u51b5\u662f\u90e8\u5206\u5199\u5165\u7684\u503c\u975e\u96f6\uff0c\u800c\u6ca1\u6709\u8bbe\u7f6e\u6574\u4e2a\u503c\u3002\u56e0\u6b64 <code>for<\/code> \u5faa\u73af\u505c\u6b62\u5faa\u73af\uff0c\u4ee3\u7801\u8fdb\u5165\u4e0b\u4e00\u884c\uff1a<\/p>\n<pre>log.println(fmt.sprint(object)) \/\/ panic here from the fmt.sprint() func<\/pre>\n<p>\u7531\u4e8e <code>object<\/code> \u4ec5<em>\u90e8\u5206<\/em>\u5199\u5165\uff0c\u56e0\u6b64\u8bbf\u95ee\u5176\u503c\u4f1a\u4ea7\u751f\u4e0d\u53ef\u9884\u6d4b\u7684\u7ed3\u679c\uff0c\u4f46\u5728\u672c\u4f8b\u4e2d\uff0c\u5b83\u4f1a\u4ea7\u751f\u6050\u614c\u3002 \uff08\u7279\u522b\u662f\uff0c\u63a5\u53e3\u7684 <em>type<\/em> \u5b57\u6bb5\u5df2\u8bbe\u7f6e\uff0c\u4f46 <em>value<\/em> \u5b57\u6bb5\u4ecd\u7136\u4e3a\u96f6\u3002\u5b9e\u9645\u7684\u6050\u614c\u6765\u81ea <code>src\/reflect\/value \u4e2d\u7684\u8fd9\u4e00\u884c\u3002 go<\/code>\uff1a<\/p>\n<pre>return *(*string)(v.ptr)<\/pre>\n<p><code>v.ptr<\/code> \u5c1a\u672a\u8bbe\u7f6e\u3002\uff09<\/p>\n<p>\u4e0d\u6e05\u695a\u4e3a\u4ec0\u4e48\u8981\u8bb0\u5f55\u8be5\u503c\uff0c\u4e5f\u4e0d\u6e05\u695a\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u901a\u4fe1\uff0c\u4f46\u5982\u679c\u60a8\u8fd9\u6837\u505a\uff0c\u5219\u9700\u8981\u9501\u5b9a\u3002\u901a\u5e38\u4e0d\u8fd9\u6837\u505a\u662f\u660e\u667a\u7684\u3002\u53e6\u8bf7\u53c2\u9605 \u81f3 \u3002<\/p>\n<p>\uff08\u6216\u8005\uff0c\u66f4\u7b80\u5355\u5730\u8bf4\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528 <code>object := &lt;-mychan<\/code> \u6765\u4ee3\u66ff\u6574\u4e2a goroutine-and-spin\uff1f\uff09<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5305\u201cfmt\u201d\u8fd0\u884c\u65f6\u95ee\u9898\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207332","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207332","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=207332"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207332\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}