{"id":38300,"date":"2024-11-26T15:52:43","date_gmt":"2024-11-26T07:52:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38300\/"},"modified":"2024-11-26T15:52:43","modified_gmt":"2024-11-26T07:52:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8go%e4%b8%ad%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38300\/","title":{"rendered":"\u5982\u4f55\u5728Go\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172795321880227.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Go\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Go\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u7f16\u5199 Go \u4ee3\u7801\u65f6\uff0c\u60a8\u4f1a\u53d1\u73b0\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u90fd\u975e\u5e38\u6709\u7528\u3002 \u60a8\u53ef\u80fd\u6b63\u5728\u89e3\u6790\u8f93\u5165\u6216\u5236\u4f5c\u6bd4\u7b80\u5355\u4e32\u8054\u66f4\u590d\u6742\u7684\u8f93\u51fa\u3002 \u60a8\u53ef\u80fd\u6b63\u5728\u4f7f\u7528\u7eaf\u5b57\u7b26\u4e32\u4ee5\u5916\u7684\u7c7b\u578b\u3002<\/p>\n<p>Go \u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4f7f\u7528\u719f\u6089\u7684 printf \u51fd\u6570\u7684\u8fc7\u7a0b\u548c\u8bed\u6cd5\uff0c\u4ece Java \u5230 Haskell \u7684\u8bed\u8a00\u4e5f\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002<\/p>\n<p>Go \u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5 <strong>fmt<\/strong> \u5305\u88f9\u3002 \u60a8\u53ef\u4ee5\u6839\u636e\u8981\u683c\u5f0f\u5316\u7684\u64cd\u4f5c\u6216\u8f93\u5165\u4f7f\u7528\u51fd\u6570\u548c\u52a8\u8bcd\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<h2>Go\u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h2>\n<p>fmt \u5305\u4e2d\u7684\u51fd\u6570\u4e0e\u5bf9\u5e94\u7684\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f8b\u5982 bash \u6216 C \u4e2d\u7684 printf \u51fd\u6570\u3002Go \u4ece C \u6d3e\u751f\u5176\u683c\u5f0f\u5316\u52a8\u8bcd\u3002<\/p>\n<p>\u4f60\u7528 <strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd<\/strong> \u4f5c\u4e3a\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u53d8\u91cf\u503c\u7684\u5360\u4f4d\u7b26\u3002 \u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5c06\u8be5\u683c\u5f0f\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9\u7c7b\u4f3c\u7684\u51fd\u6570 <strong>\u6253\u5370<\/strong>\uff0c\u4ee5\u53ca\u4e0e\u8fd9\u4e9b\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p>\u60a8\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd\u4e0e <strong>\u6253\u5370<\/strong> \u548c <strong>\u6253\u5370<\/strong> \u65b9\u6cd5\u3002 \u60a8\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u4ee5\u4e0b\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528 <strong>\u6253\u5370<\/strong> \u548c <strong>\u51b2\u523a<\/strong>.<\/p>\n<pre>fmt.Println(\"This is a test %v\", 90)\nfmt.Printf(\"This is a test %v\", 90)\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9 <strong>%v<\/strong> \u52a8\u8bcd\u4ee5\u5176\u9ed8\u8ba4\u683c\u5f0f\u6253\u5370\u4efb\u4f55\u503c\u3002 \u8fd9 <strong>\u6253\u5370<\/strong> \u65b9\u6cd5\u4e0d\u8bc6\u522b\u52a8\u8bcd\u5e76\u6253\u5370\u5b83\u6536\u5230\u7684\u4efb\u4f55\u53c2\u6570\u3002 \u8fd9 <strong>\u6253\u5370<\/strong> \u548c <strong>\u51b2\u523a<\/strong> \u51fd\u6570\u90fd\u683c\u5f0f\u5316\u60a8\u4f20\u9012\u7ed9\u5b83\u4eec\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\u3002<\/p>\n<h2>fmt \u5305\u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570<\/h2>\n<p>\u5728 Go \u7f16\u7a0b\u8bed\u8a00\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u9700\u8981\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\u548c\u52a8\u8bcd\u3002 \u8be5\u51fd\u6570\u8fd4\u56de\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\uff0c\u52a8\u8bcd\u662f\u5b57\u7b26\u4e32\u8f93\u5165\u7684\u5360\u4f4d\u7b26\u3002<\/p>\n<p>\u8fd9 <strong>\u6253\u5370<\/strong> \u65b9\u6cd5\u6839\u636e\u683c\u5f0f\u8bf4\u660e\u7b26\u683c\u5f0f\u5316\u8f93\u5165\u5e76\u8fd4\u56de\u5199\u5165\u7684\u5b57\u8282\u6570\u6216\u9519\u8bef\u6570\u3002<\/p>\n<pre>fmt.Printf(\"This is a test %v\", 90)\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6309\u7167\u60ef\u4f8b\uff0c\u60a8\u5728\u4f7f\u7528 <strong>\u6253\u5370<\/strong> \u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9 <strong>\u51b2\u523a<\/strong> \u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u683c\u5f0f\u5316\uff0c\u5e76\u5c06\u7ed3\u679c\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002<\/p>\n<pre>var result = fmt.Sprintf(\"This is a test %v\", 90)\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9 <strong>\u6253\u5370<\/strong> \u65b9\u6cd5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u5199\u5165\u5199\u5165\u5668\uff08\u5b9e\u73b0 <strong>io.Writer<\/strong> \u754c\u9762\uff09<\/p>\n<pre>\nresult, err = fmt.Fprintf(writer, \"This is a test %v\", 90)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9 <strong>\u626b\u63cf\u4eea<\/strong> \u65b9\u6cd5\u4ece\u9605\u8bfb\u5668\u626b\u63cf\u5e76\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/p>\n<pre>var take string<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>readString := strings.NewReader(\u201cThis is a test\u201d)<\/p>\n<pre><\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>read, err := fmt.Fscanf(reader, \u201c%v\u201d, &amp;take)<\/p>\n<pre><\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c <strong>\u626b\u63cf\u4eea<\/strong> \u5c06\u6765\u81ea\u9605\u8bfb\u5668\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a <strong>\u62ff<\/strong> \u53d8\u91cf\uff0c\u5e76\u4e14 <strong>\u8bfb<\/strong> \u53d8\u91cf\u4fdd\u5b58\u683c\u5f0f\u7684\u7ed3\u679c\u3002<\/p>\n<h2>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd<\/h2>\n<p>Go \u63d0\u4f9b\u4e86\u8bb8\u591a\u683c\u5f0f\u5316\u52a8\u8bcd\uff0c\u60a8\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<p>\u6709\u4e00\u822c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd\uff0c\u5982 <strong>%v<\/strong> \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\u793a\u4f8b\u4e2d\u7684\u52a8\u8bcd\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528\u901a\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd\u6765\u683c\u5f0f\u5316\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>%#v<\/strong> \u8f93\u51fa\u4efb\u4f55\u503c\u7684\u52a8\u8bcd\uff0c <strong>%+v<\/strong> \u5bf9\u4e8e\u7ed3\u6784\uff0c <strong>%T<\/strong> \u52a8\u8bcd\u8868\u793a\u4efb\u4f55\u503c\u7684\u7c7b\u578b\uff0c\u4ee5\u53ca <strong>%%<\/strong> \u6ca1\u6709\u4ef7\u503c\u7684\u52a8\u8bcd\u3002<\/p>\n<pre>type any struct { \nname string\nage int\nisLoggedIn bool\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>var instance = any {<br \/>name: \u201cJohn Doe\u201d,<br \/>age: 34,<br \/>isLoggedIn: true,<br \/>}<\/p>\n<pre><\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>var result = fmt.Sprintf(\u201cThis is a struct formatting example %+v\u201d, instance)<br \/>fmt.Println(result)<\/p>\n<pre><\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9 <strong>\u7ed3\u679c<\/strong> \u53d8\u91cf\u4fdd\u5b58\u5b9e\u4f8b\u5316\u7ed3\u6784\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u4f60\u6253\u5370\u51fa\u6765\uff0c\u5b83\u5e94\u8be5\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>This is a struct formatting example {name:John Doe age:34 isLoggedIn:true}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6709\u7528\u4e8e\u683c\u5f0f\u5316\u7279\u5b9a Go \u539f\u751f\u6570\u636e\u7c7b\u578b\u7684\u52a8\u8bcd\uff0c\u5305\u62ec\u901a\u9053\u548c\u6307\u9488\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u52a8\u8bcd<\/th>\n<th>\u529f\u80fd\u6027<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>%t<\/td>\n<td>\u5e03\u5c14\u503c\u3002<\/td>\n<\/tr>\n<tr>\n<td>%d<\/td>\n<td>int\u3001int8 \u7b49<\/td>\n<\/tr>\n<tr>\n<td>%d, %#x \u5982\u679c\u7528 %#v \u6253\u5370<\/td>\n<td>uint\u3001uint8 \u7b49<\/td>\n<\/tr>\n<tr>\n<td>\uff05G<\/td>\n<td>float32\u3001complex64 \u7b49<\/td>\n<\/tr>\n<tr>\n<td>%s<\/td>\n<td>\u7ec6\u7ef3\u3002<\/td>\n<\/tr>\n<tr>\n<td>%p<\/td>\n<td>\u9648<\/td>\n<\/tr>\n<tr>\n<td>%P<\/td>\n<td>\u6307\u9488\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u60a8\u9700\u8981\u786e\u4fdd\u4e0d\u4f1a\u5728\u52a8\u8bcd\u4e0a\u72af\u9519\u8bef\uff0c\u56e0\u4e3a\u5b83\u4eec\u533a\u5206\u5927\u5c0f\u5199\uff0c\u4f8b\u5982 <strong>\u9648<\/strong> \u548c <strong>\u6307\u9488<\/strong> \u52a8\u8bcd\u3002<\/p>\n<h2>\u683c\u5f0f\u5316\u6574\u6570\u548c\u6d6e\u70b9\u6570<\/h2>\n<p>\u6709\u7528\u4e8e\u683c\u5f0f\u5316\u4e0d\u540c\u57fa\u6570\u7684\u6574\u6570\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u52a8\u8bcd\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u6765\u683c\u5f0f\u5316\u6574\u6570<\/p>\n<table>\n<thead>\n<tr>\n<th>\u52a8\u8bcd<\/th>\n<th>\u529f\u80fd\u6027<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>%b<\/td>\n<td>\u57fa\u57302<\/td>\n<\/tr>\n<tr>\n<td>\uff05C<\/td>\n<td>\u7531\u76f8\u5e94\u7684 Unicode \u4ee3\u7801\u70b9\u8868\u793a\u7684\u5b57\u7b26\u3002<\/td>\n<\/tr>\n<tr>\n<td>%d<\/td>\n<td>\u57fa\u6570 10\u3002<\/td>\n<\/tr>\n<tr>\n<td>%o<\/td>\n<td>\u57fa\u5730 8.<\/td>\n<\/tr>\n<tr>\n<td>%O<\/td>\n<td>\u4ee5 8 \u4e3a\u57fa\u6570\uff0c\u524d\u7f00\u4e3a 0o\u3002<\/td>\n<\/tr>\n<tr>\n<td>%q<\/td>\n<td>\u4f7f\u7528 Go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49\u7684\u5355\u5f15\u53f7\u5b57\u7b26\u6587\u5b57\u3002<\/td>\n<\/tr>\n<tr>\n<td>\uff05X<\/td>\n<td>\u4ee5 16 \u4e3a\u57fa\u6570\uff0c\u7528\u5c0f\u5199\u5b57\u6bcd\u8868\u793a af\u3002<\/td>\n<\/tr>\n<tr>\n<td>\uff05X<\/td>\n<td>\u4ee5 16 \u4e3a\u57fa\u6570\uff0cAF \u4e3a\u5927\u5199\u5b57\u6bcd\u3002<\/td>\n<\/tr>\n<tr>\n<td>%U<\/td>\n<td>Unicode \u683c\u5f0f\uff1aU+1234\uff1b \u4e0e\u201cU+%04X\u201d\u76f8\u540c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>%d<\/strong> \u52a8\u8bcd\uff1a<\/p>\n<pre>var result = fmt.Sprintf(\"This is an integer formatting example %d\", 90)\nfmt.Println(result)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e9b\u662f\u683c\u5f0f\u5316\u6d6e\u70b9\u6570\u7684\u52a8\u8bcd\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u52a8\u8bcd<\/th>\n<th>\u529f\u80fd\u6027<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>%b<\/td>\n<td>\u4ee5 strconv \u7684\u65b9\u5f0f\uff0c\u6307\u6570\u4e3a 2 \u7684\u65e0\u5341\u8fdb\u5236\u79d1\u5b66\u8bb0\u6570\u6cd5\u3002 FormatFloat \u4f7f\u7528 \u2018b\u2019 \u683c\u5f0f\uff0c\u4f8b\u5982 -123456p-78<\/td>\n<\/tr>\n<tr>\n<td>%e<\/td>\n<td>\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u4f8b\u5982 -1.234456e+78<\/td>\n<\/tr>\n<tr>\n<td>%E<\/td>\n<td>\u5c0f\u6570\u70b9\u4f46\u6ca1\u6709\u6307\u6570\uff0c\u4f8b\u5982 123.456<\/td>\n<\/tr>\n<tr>\n<td>\uff05F<\/td>\n<td>\u5c0f\u6570\u70b9\u4f46\u6ca1\u6709\u6307\u6570\uff0c\u4f8b\u5982 123.456<\/td>\n<\/tr>\n<tr>\n<td>\uff05F<\/td>\n<td>%f \u7684\u540c\u4e49\u8bcd\u3002<\/td>\n<\/tr>\n<tr>\n<td>\uff05G<\/td>\n<td>%e \u7528\u4e8e\u5927\u6307\u6570\uff0c%f \u5426\u5219\u3002 \u7cbe\u5ea6\u4f4e\u4e8e\u3002<\/td>\n<\/tr>\n<tr>\n<td>\uff05G<\/td>\n<td>%E \u7528\u4e8e\u5927\u6307\u6570\uff0c%F \u5426\u5219<\/td>\n<\/tr>\n<tr>\n<td>\uff05X<\/td>\n<td>\u5341\u516d\u8fdb\u5236\u8868\u793a\u6cd5\uff08\u5177\u6709\u4e24\u4e2a\u6307\u6570\u7684\u5341\u8fdb\u5236\u5e42\uff09\uff0c\u4f8b\u5982 -0x1.23abcp+20\u3002<\/td>\n<\/tr>\n<tr>\n<td>\uff05X<\/td>\n<td>\u5927\u5199\u5341\u516d\u8fdb\u5236\u8868\u793a\u6cd5\uff0c\u4f8b\u5982 -0X1.23ABCP+20\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u662f\u4e00\u4e2a\u683c\u5f0f\u5316\u5c0f\u6570\u70b9\u7684\u793a\u4f8b\uff0c\u6ca1\u6709\u6307\u6570 <strong>\uff05F<\/strong> \u52a8\u8bcd\u3002<\/p>\n<pre>var result = fmt.Sprintf(\"This is a floating point formatting example %f\", 432.9503)\nfmt.Println(result)\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u7c7b\u578b\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u4f7f\u7528\u4e00\u822c\u52a8\u8bcd\u3002<\/p>\n<h2>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5b57\u8282<\/h2>\n<p>Go \u4e2d\u7684\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5207\u7247\u7c7b\u578b\u975e\u5e38\u76f8\u4f3c\u3002 \u8fd9\u4e9b\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u7684\u6807\u5fd7\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u52a8\u8bcd<\/th>\n<th>\u529f\u80fd\u6027<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>%s<\/td>\n<td>\u5b57\u7b26\u4e32\u6216\u5207\u7247\u7684\u672a\u89e3\u91ca\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>%q<\/td>\n<td>\u4f7f\u7528 Go \u8bed\u6cd5\u5b89\u5168\u8f6c\u4e49\u7684\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>\uff05X<\/td>\n<td>\u57fa\u6570 16\uff0c\u5c0f\u5199\uff0c\u6bcf\u5b57\u8282\u4e24\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>\uff05X<\/td>\n<td>\u4ee5 16 \u4e3a\u57fa\u6570\uff0c\u5927\u5199\uff0c\u6bcf\u5b57\u8282\u4e24\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7528 <strong>%s<\/strong> \u52a8\u8bcd\u3002<\/p>\n<pre>var score = \"example\"\nvar result = fmt.Sprintf(\"This is a string formatting example %s\", score)\nfmt.Println(result)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>fmt \u5305\u5bf9\u4e8e Python \u7f16\u7a0b\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684<\/h2>\n<p>\u8fd9 <strong>fmt<\/strong> \u5305\u5305\u542b\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6240\u9700\u7684\u5927\u90e8\u5206\u529f\u80fd\u3002 Go \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a <strong>\u5b57\u7b26\u4e32<\/strong> \u5b57\u7b26\u4e32\u64cd\u4f5c\u5305\u548c\u4e00\u4e2a <strong>\u65e5\u5fd7<\/strong> \u53ef\u4ee5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4ee5\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u7684\u5305\u3002<\/p>\n<p>\u8fd9 <strong>fmt<\/strong> \u5305\u5177\u6709\u5b9e\u73b0\u7684\u529f\u80fd <strong>io.Writer<\/strong> \u548c <strong>io.Reader<\/strong> \u63a5\u53e3\u3002 \u60a8\u4f1a\u53d1\u73b0\u5b83\u5bf9\u8bb8\u591a\u7528\u4f8b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u6784\u5efa Web \u548c\u547d\u4ee4\u884c\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Go\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f16\u5199 Go \u4ee3\u7801\u65f6\uff0c\u60a8\u4f1a\u53d1\u73b0\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u90fd\u975e\u5e38\u6709\u7528\u3002 \u60a8\u53ef\u80fd\u6b63\u5728\u89e3\u6790\u8f93\u5165\u6216\u5236\u4f5c\u6bd4\u7b80\u5355\u4e32\u8054\u66f4\u590d\u6742\u7684\u8f93\u51fa\u3002 \u60a8\u53ef\u80fd\u6b63\u5728\u4f7f\u7528\u7eaf\u5b57\u7b26\u4e32\u4ee5\u5916\u7684\u7c7b\u578b\u3002 Go \u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4f7f\u7528\u719f\u6089\u7684 printf \u51fd\u6570\u7684\u8fc7\u7a0b\u548c\u8bed\u6cd5\uff0c\u4ece Java \u5230 Haskell \u7684\u8bed\u8a00\u4e5f\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u3002 Go \u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5 fmt \u5305\u88f9\u3002 \u60a8\u53ef\u4ee5\u6839\u636e\u8981\u683c\u5f0f\u5316\u7684\u64cd\u4f5c\u6216\u8f93\u5165\u4f7f\u7528\u51fd\u6570\u548c\u52a8\u8bcd\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002 Go\u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316 fmt \u5305\u4e2d\u7684\u51fd\u6570\u4e0e\u5bf9\u5e94\u7684\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f8b\u5982 bash \u6216 C \u4e2d\u7684 printf \u51fd\u6570\u3002Go \u4ece C \u6d3e\u751f\u5176\u683c\u5f0f\u5316\u52a8\u8bcd\u3002 \u4f60\u7528 \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd \u4f5c\u4e3a\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u53d8\u91cf\u503c\u7684\u5360\u4f4d\u7b26\u3002 \u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5c06\u8be5\u683c\u5f0f\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9\u7c7b\u4f3c\u7684\u51fd\u6570 \u6253\u5370\uff0c\u4ee5\u53ca\u4e0e\u8fd9\u4e9b\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\u3002 \u60a8\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u52a8\u8bcd\u4e0e \u6253\u5370 \u548c \u6253\u5370 \u65b9\u6cd5\u3002 \u60a8\u53ef\u4ee5\u5c06\u5b83\u4eec\u4e0e\u4ee5\u4e0b\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528 \u6253\u5370 \u548c \u51b2\u523a. fmt.Println(&#8220;This is a test %v&#8221;, 90) fmt.Printf(&#8220;This is a test %v&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-38300","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38300"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38300\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}