{"id":208120,"date":"2025-07-08T13:33:44","date_gmt":"2025-07-08T05:33:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208120\/"},"modified":"2025-07-08T13:33:44","modified_gmt":"2025-07-08T05:33:44","slug":"%e7%94%a8-go-%e8%af%ad%e6%b3%95%e5%ae%89%e5%85%a8%e8%bd%ac%e4%b9%89%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208120\/","title":{"rendered":"\u201c\u7528 Go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49\u201d\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},"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>\u201c\u7528 Go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49\u201d\u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 10:00:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u201c\u7528 Go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49\u201d\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>go \u7684 <code>fmt<\/code> \u5305\u5c06 <code>%q<\/code> \uff08\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff09\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<pre>\n %q      a double-quoted string safely escaped with go syntax\n<\/pre>\n<p><strong><em>\u4f7f\u7528 go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49<\/em><\/strong>\u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/p>\n<p>\u4e00\u4e9b\u5b9e\u9a8c\u8868\u660e\u5b83\u4fdd\u7559\u4e86\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u7684\u8f6c\u4e49\u5e8f\u5217\uff1a<\/p>\n<pre>s := \"This has \\\"quotes\\\" in it\"\nfmt.Printf(\"%q\\n\", s)             \/\/ output: \"This has \\\"quotes\\\" in it\"<\/pre>\n<p>\u5b83\u8fd8\u6709\u4ec0\u4e48\u4f5c\u7528\u5417\uff1f\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u60a8\u53ef\u80fd\u60f3\u4f7f\u7528\u5b83\uff1f\u6211\u731c\u4e5f\u8bb8\u662f\u5728\u751f\u6210 go \u4ee3\u7801\u7684\u6a21\u677f\u4e2d\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u610f\u5473\u7740\u683c\u5f0f\u5316\u7684\u8f93\u51fa\u4f1a\u88ab\u6b63\u786e\u8f6c\u4e49\uff0c\u53ef\u4ee5\u5728go\u6e90\u7801\u4e2d\u590d\u5236\u548c\u4f7f\u7528<\/p>\n<p>\u683c\u5f0f\u793a\u4f8b<\/p>\n<pre>\"abc\"          =&gt; `\"abc\"`\n []byte(\"abc\")  =&gt; `\"abc\"`\n \"\"             =&gt; `\"\"`\n \"\\\"\"           =&gt; `\"\\\"\"`\n `\\n`           =&gt; `\"\\\\n\"`\n renamedbytes([]byte(\"hello\")) =&gt; `\"hello\"`\n []renameduint8{'h', 'e', 'l', 'l', 'o'} =&gt; `\"hello\"`\n reflect.valueof(\"hello\") =&gt; `\"hello\"`<\/pre>\n<p>\u89e3\u91ca\u4e0a\u8ff0\u5185\u5bb9\u7684\u4ee3\u7801<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"reflect\"\n)\n\nfunc main() {\n    type renamedBytes []byte\n    type renamedUint8 uint8\n\n    fmt.Printf(\"%q\\n\", \"abc\")\n    fmt.Printf(\"%q\\n\", []byte(\"abc\"))\n    fmt.Printf(\"%q\\n\", \"\\n\")\n    fmt.Printf(\"%q\\n\", []renamedUint8{'h', 'e', 'l', 'l', 'o'})\n    fmt.Printf(\"%q\\n\", renamedBytes([]byte(\"hello\")))\n    fmt.Printf(\"%q\\n\", reflect.ValueOf(\"hello\"))\n}<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u201c\u7528 Go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49\u201d\u662f\u4ec0\u4e48\u610f\u601d\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/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-208120","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208120","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=208120"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208120\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}