{"id":2620,"date":"2018-12-27T17:08:44","date_gmt":"2018-12-27T09:08:44","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2620"},"modified":"2018-12-27T17:08:44","modified_gmt":"2018-12-27T09:08:44","slug":"go-%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%9aprintln-%e4%b8%8e-printf-%e7%9a%84%e5%8c%ba%e5%88%ab%ef%bc%8c%e4%bb%a5%e5%8f%8a-printf-%e7%9a%84%e8%af%a6%e7%bb%86%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2620","title":{"rendered":"Go \u5b66\u4e60\u7b14\u8bb0\uff1aPrintln \u4e0e Printf \u7684\u533a\u522b\uff0c\u4ee5\u53ca Printf \u7684\u8be6\u7ec6\u7528\u6cd5"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix csdn-tracking-statistics\" data-pid=\"blog\" data-mod=\"popu_307\" data-dsm=\"post\">\n<div class=\"article-copyright\">Println \u4e0ePrintf \u90fd\u662ffmt \u5305\u4e2d\u7684\u516c\u5171\u65b9\u6cd5\uff0c\u5728\u9700\u8981\u6253\u5370\u4fe1\u606f\u65f6\u9700\u8981\u7528\u5230\u8fd9\u4e8c\u4e2a\u51fd\u6570\uff0c\u90a3\u4e48\u8fd9\u4e8c\u4e2a\u51fd\u6570\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/div>\n<div id=\"content_views\" class=\"markdown_views\">\n<p><strong>Println :\u53ef\u4ee5\u6253\u5370\u51fa\u5b57\u7b26\u4e32\uff0c\u548c\u53d8\u91cf<\/strong><br \/>\n<strong>Printf : \u53ea\u53ef\u4ee5\u6253\u5370\u51fa\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32,\u53ef\u4ee5\u8f93\u51fa\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4e0d\u53ef\u4ee5\u8f93\u51fa\u6574\u5f62\u53d8\u91cf\u548c\u6574\u5f62<\/strong><\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u4fe1\u606f\u65f6\u4e00\u822c\u9009\u62e9 Printf\uff0c\u5176\u4ed6\u65f6\u5019\u7528 Println \u5c31\u53ef\u4ee5\u4e86\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">a := 10\r\nfmt.Println(a)\u3000\u3000\/\/right\r\nfmt.Println(\"abc\")\u3000\u3000\/\/right\r\nfmt.Printf(\"%d\",a)\u3000\u3000\/\/right\r\nfmt.Printf(a)\u3000\u3000\/\/error<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"printf-\u8be6\u7ec6\u7528\u6cd5\">Printf \u8be6\u7ec6\u7528\u6cd5<\/h3>\n<p>Printf \u5728\u4f7f\u7528\u65f6\u9700\u8981\u7ed9\u5b83\u6307\u5b9a\u4e00\u4e2a\u683c\u5f0f\u5316\u89c4\u5219\uff0c\u5373\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u90a3\u4e48\u4e00\u5171\u6709\u591a\u5c11\u79cd\u683c\u5f0f\u5462\uff0c\u7528\u7684\u65f6\u5019\u9700\u8981\u600e\u4e48\u5199\u5462\uff1f<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package main\r\nimport \"fmt\"\r\nimport \"os\"\r\ntype point struct {\r\n    x, y int\r\n}\r\nfunc main() {\r\n    \/\/Go \u4e3a\u5e38\u89c4 Go \u503c\u7684\u683c\u5f0f\u5316\u8bbe\u8ba1\u63d0\u4f9b\u4e86\u591a\u79cd\u6253\u5370\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u8fd9\u91cc\u6253\u5370\u4e86 point \u7ed3\u6784\u4f53\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\r\n    p := point{1, 2}\r\n    fmt.Printf(\"%v\\n\", p) \/\/ {1 2}\r\n    \/\/\u5982\u679c\u503c\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c%+v \u7684\u683c\u5f0f\u5316\u8f93\u51fa\u5185\u5bb9\u5c06\u5305\u62ec\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u540d\u3002\r\n    fmt.Printf(\"%+v\\n\", p) \/\/ {x:1 y:2}\r\n    \/\/%#v \u5f62\u5f0f\u5219\u8f93\u51fa\u8fd9\u4e2a\u503c\u7684 Go \u8bed\u6cd5\u8868\u793a\u3002\u4f8b\u5982\uff0c\u503c\u7684\u8fd0\u884c\u6e90\u4ee3\u7801\u7247\u6bb5\u3002\r\n    fmt.Printf(\"%#v\\n\", p) \/\/ main.point{x:1, y:2}\r\n    \/\/\u9700\u8981\u6253\u5370\u503c\u7684\u7c7b\u578b\uff0c\u4f7f\u7528 %T\u3002\r\n    fmt.Printf(\"%T\\n\", p) \/\/ main.point\r\n    \/\/\u683c\u5f0f\u5316\u5e03\u5c14\u503c\u662f\u7b80\u5355\u7684\u3002\r\n    fmt.Printf(\"%t\\n\", true)\r\n    \/\/\u683c\u5f0f\u5316\u6574\u5f62\u6570\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u4f7f\u7528 %d\u8fdb\u884c\u6807\u51c6\u7684\u5341\u8fdb\u5236\u683c\u5f0f\u5316\u3002\r\n    fmt.Printf(\"%d\\n\", 123)\r\n    \/\/\u8fd9\u4e2a\u8f93\u51fa\u4e8c\u8fdb\u5236\u8868\u793a\u5f62\u5f0f\u3002\r\n    fmt.Printf(\"%b\\n\", 14)\r\n    \/\/\u8fd9\u4e2a\u8f93\u51fa\u7ed9\u5b9a\u6574\u6570\u7684\u5bf9\u5e94\u5b57\u7b26\u3002\r\n    fmt.Printf(\"%c\\n\", 33)\r\n    \/\/%x \u63d0\u4f9b\u5341\u516d\u8fdb\u5236\u7f16\u7801\u3002\r\n    fmt.Printf(\"%x\\n\", 456)\r\n    \/\/\u5bf9\u4e8e\u6d6e\u70b9\u578b\u540c\u6837\u6709\u5f88\u591a\u7684\u683c\u5f0f\u5316\u9009\u9879\u3002\u4f7f\u7528 %f \u8fdb\u884c\u6700\u57fa\u672c\u7684\u5341\u8fdb\u5236\u683c\u5f0f\u5316\u3002\r\n    fmt.Printf(\"%f\\n\", 78.9)\r\n    \/\/%e \u548c %E \u5c06\u6d6e\u70b9\u578b\u683c\u5f0f\u5316\u4e3a\uff08\u7a0d\u5fae\u6709\u4e00\u70b9\u4e0d\u540c\u7684\uff09\u79d1\u5b66\u6280\u79d1\u5b66\u8bb0\u6570\u6cd5\u8868\u793a\u5f62\u5f0f\u3002\r\n    fmt.Printf(\"%e\\n\", 123400000.0)\r\n    fmt.Printf(\"%E\\n\", 123400000.0)\r\n    \/\/\u4f7f\u7528 %s \u8fdb\u884c\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u8f93\u51fa\u3002\r\n    fmt.Printf(\"%s\\n\", \"\\\"string\\\"\")\r\n    \/\/\u50cf Go \u6e90\u4ee3\u7801\u4e2d\u90a3\u6837\u5e26\u6709\u53cc\u5f15\u53f7\u7684\u8f93\u51fa\uff0c\u4f7f\u7528 %q\u3002\r\n    fmt.Printf(\"%q\\n\", \"\\\"string\\\"\")\r\n    \/\/\u548c\u4e0a\u9762\u7684\u6574\u5f62\u6570\u4e00\u6837\uff0c%x \u8f93\u51fa\u4f7f\u7528 base-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u5b57\u8282\u4f7f\u7528 2 \u4e2a\u5b57\u7b26\u8868\u793a\u3002\r\n    fmt.Printf(\"%x\\n\", \"hex this\")\r\n    \/\/\u8981\u8f93\u51fa\u4e00\u4e2a\u6307\u9488\u7684\u503c\uff0c\u4f7f\u7528 %p\u3002\r\n    fmt.Printf(\"%p\\n\", &amp;p)\r\n    \/\/\u5f53\u8f93\u51fa\u6570\u5b57\u7684\u65f6\u5019\uff0c\u4f60\u5c06\u7ecf\u5e38\u60f3\u8981\u63a7\u5236\u8f93\u51fa\u7ed3\u679c\u7684\u5bbd\u5ea6\u548c\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5728 % \u540e\u9762\u4f7f\u7528\u6570\u5b57\u6765\u63a7\u5236\u8f93\u51fa\u5bbd\u5ea6\u3002\u9ed8\u8ba4\u7ed3\u679c\u4f7f\u7528\u53f3\u5bf9\u9f50\u5e76\u4e14\u901a\u8fc7\u7a7a\u683c\u6765\u586b\u5145\u7a7a\u767d\u90e8\u5206\u3002\r\n    fmt.Printf(\"|%6d|%6d|\\n\", 12, 345)\r\n    \/\/\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u6d6e\u70b9\u578b\u7684\u8f93\u51fa\u5bbd\u5ea6\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7 \u5bbd\u5ea6.\u7cbe\u5ea6 \u7684\u8bed\u6cd5\u6765\u6307\u5b9a\u8f93\u51fa\u7684\u7cbe\u5ea6\u3002\r\n    fmt.Printf(\"|%6.2f|%6.2f|\\n\", 1.2, 3.45)\r\n    \/\/\u8981\u6700\u5bf9\u9f50\uff0c\u4f7f\u7528 - \u6807\u5fd7\u3002\r\n    fmt.Printf(\"|%-6.2f|%-6.2f|\\n\", 1.2, 3.45)\r\n    \/\/\u4f60\u4e5f\u8bb8\u4e5f\u60f3\u63a7\u5236\u5b57\u7b26\u4e32\u8f93\u51fa\u65f6\u7684\u5bbd\u5ea6\uff0c\u7279\u522b\u662f\u8981\u786e\u4fdd\u4ed6\u4eec\u5728\u7c7b\u8868\u683c\u8f93\u51fa\u65f6\u7684\u5bf9\u9f50\u3002\u8fd9\u662f\u57fa\u672c\u7684\u53f3\u5bf9\u9f50\u5bbd\u5ea6\u8868\u793a\u3002\r\n    fmt.Printf(\"|%6s|%6s|\\n\", \"foo\", \"b\")\r\n    \/\/\u8981\u5de6\u5bf9\u9f50\uff0c\u548c\u6570\u5b57\u4e00\u6837\uff0c\u4f7f\u7528 - \u6807\u5fd7\u3002\r\n    fmt.Printf(\"|%-6s|%-6s|\\n\", \"foo\", \"b\")\r\n    \/\/\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u8fc7 Printf\u4e86\uff0c\u5b83\u901a\u8fc7 os.Stdout\u8f93\u51fa\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u3002Sprintf \u5219\u683c\u5f0f\u5316\u5e76\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u800c\u4e0d\u5e26\u4efb\u4f55\u8f93\u51fa\u3002\r\n    s := fmt.Sprintf(\"a %s\", \"string\")\r\n    fmt.Println(s)\r\n    \/\/\u4f60\u53ef\u4ee5\u4f7f\u7528 Fprintf \u6765\u683c\u5f0f\u5316\u5e76\u8f93\u51fa\u5230 io.Writers\u800c\u4e0d\u662f os.Stdout\u3002\r\n    fmt.Fprintf(os.Stderr, \"an %s\\n\", \"error\")\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u79cd\u7c7b\u8fd8\u662f\u633a\u591a\u7684\uff0c\u4f46\u5e73\u65f6\u7ecf\u5e38\u7528\u7684\u4e5f\u5c31\u524d\u9762\u51e0\u79cd\u800c\u5df2\uff0c\u6216\u8005\u53ef\u4ee5\u8bf4\uff0c\u7528\u7684\u6700\u591a\u7684\u5c31\u662f\u7b2c\u4e00\u4e2a\uff0c\uff0c\uff0c<\/p>\n<p>\u5173\u4e8e Printf \u66f4\u591a\u7684\u7528\u6cd5\u548c\u539f\u7406\u65b9\u9762\u7684\u89e3\u91ca\u53ef\u4ee5\u770b\u8fd9\u4e8c\u7bc7\u5e16\u5b50<br \/>\n<a href=\"http:\/\/www.jianshu.com\/p\/9637c18d5f01\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/www.jianshu.com\/p\/9637c18d5f01<\/a><br \/>\n<a href=\"http:\/\/blog.csdn.net\/chenbaoke\/article\/details\/39932845\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/blog.csdn.net\/chenbaoke\/article\/details\/39932845<\/a><\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Println \u4e0ePrintf \u90fd\u662ffm<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[122],"class_list":["post-2620","post","type-post","status-publish","format-standard","hentry","tag-06-godocker"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2620"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2620\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}