Go 18.0 专业五


fmt

<ol> <li> <h1>fmt</h1> </li> </ol> <p>fmt包实现了类似C语言printf和scanf的格式化I/O。主要分为向外输出内容和获取输入内容两大部分。</p> <h3>1.1.1. 向外输出</h3> <p>标准库fmt提供了以下几种输出相关函数。</p> <h4>Print</h4> <p>Print系列函数会将内容输出到系统的标准输出,区别在于Print函数直接输出内容,Printf函数支持格式化输出字符串,Println函数会在输出内容的结尾添加一个换行符。</p> <pre><code>func Print(a ...interface{}) (n int, err error) func Printf(format string, a ...interface{}) (n int, err error) func Println(a ...interface{}) (n int, err error)</code></pre> <p>举个简单的例子:</p> <pre><code>func main() { fmt.Print(&amp;quot;在终端打印该信息。&amp;quot;) name := &amp;quot;枯藤&amp;quot; fmt.Printf(&amp;quot;我是:%s\n&amp;quot;, name) fmt.Println(&amp;quot;在终端打印单独一行显示&amp;quot;) }</code></pre> <p>执行上面的代码输出:</p> <pre><code> 在终端打印该信息。我是:枯藤 在终端打印单独一行显示</code></pre> <h4>Sprint</h4> <p>Sprint系列函数会把传入的数据生成并返回一个字符串。</p> <pre><code>func Sprint(a ...interface{}) string func Sprintf(format string, a ...interface{}) string func Sprintln(a ...interface{}) string</code></pre> <p>简单的示例代码如下:</p> <pre><code>s1 := fmt.Sprint(&amp;quot;枯藤&amp;quot;) name := &amp;quot;枯藤&amp;quot; age := 18 s2 := fmt.Sprintf(&amp;quot;name:%s,age:%d&amp;quot;, name, age) s3 := fmt.Sprintln(&amp;quot;枯藤&amp;quot;) fmt.Println(s1, s2, s3)</code></pre> <h3>1.1.2. 格式化占位符</h3> <p><code>*printf</code>系列函数都支持format格式化参数,在这里我们按照占位符将被替换的变量类型划分,方便查询和记忆。</p> <h4>通用占位符</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%v</td> <td>值的默认格式表示</td> </tr> <tr> <td>%+v</td> <td>类似%v,但输出结构体时会添加字段名</td> </tr> <tr> <td>%#v</td> <td>值的Go语法表示</td> </tr> <tr> <td>%T</td> <td>打印值的类型</td> </tr> <tr> <td>%%</td> <td>百分号</td> </tr> </tbody> </table> <p>示例代码如下:</p> <pre><code>fmt.Printf(&amp;quot;%v\n&amp;quot;, 100) fmt.Printf(&amp;quot;%v\n&amp;quot;, false) o := struct{ name string }{&amp;quot;枯藤&amp;quot;} fmt.Printf(&amp;quot;%v\n&amp;quot;, o) fmt.Printf(&amp;quot;%#v\n&amp;quot;, o) fmt.Printf(&amp;quot;%T\n&amp;quot;, o) fmt.Printf(&amp;quot;100%%\n&amp;quot;)</code></pre> <p>输出结果如下:</p> <pre><code>100 false {枯藤} struct { name string }{name:&amp;quot;枯藤&amp;quot;} struct { name string } 100%</code></pre> <h4>布尔型</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%t</td> <td>true或false</td> </tr> </tbody> </table> <h4>整型</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%b</td> <td>表示为二进制</td> </tr> <tr> <td>%c</td> <td>该值对应的unicode码值</td> </tr> <tr> <td>%d</td> <td>表示为十进制</td> </tr> <tr> <td>%o</td> <td>表示为八进制</td> </tr> <tr> <td>%x</td> <td>表示为十六进制,使用a-f</td> </tr> <tr> <td>%X</td> <td>表示为十六进制,使用A-F</td> </tr> <tr> <td>%U</td> <td>表示为Unicode格式:U+1234,等价于”U+%04X”</td> </tr> <tr> <td>%q</td> <td>该值对应的单引号括起来的go语法字符字面值,必要时会采用安全的转义表示</td> </tr> </tbody> </table> <p>示例代码如下:</p> <pre><code>n := 65 fmt.Printf(&amp;quot;%b\n&amp;quot;, n) fmt.Printf(&amp;quot;%c\n&amp;quot;, n) fmt.Printf(&amp;quot;%d\n&amp;quot;, n) fmt.Printf(&amp;quot;%o\n&amp;quot;, n) fmt.Printf(&amp;quot;%x\n&amp;quot;, n) fmt.Printf(&amp;quot;%X\n&amp;quot;, n)</code></pre> <p>输出结果如下:</p> <pre><code> 1000001 A 65 101 41 41</code></pre> <h4>浮点数与复数</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%b</td> <td>无小数部分、二进制指数的科学计数法,如-123456p-78</td> </tr> <tr> <td>%e</td> <td>科学计数法,如-1234.456e+78</td> </tr> <tr> <td>%E</td> <td>科学计数法,如-1234.456E+78</td> </tr> <tr> <td>%f</td> <td>有小数部分但无指数部分,如123.456</td> </tr> <tr> <td>%F</td> <td>等价于%f</td> </tr> <tr> <td>%g</td> <td>根据实际情况采用%e或%f格式(以获得更简洁、准确的输出)</td> </tr> <tr> <td>%G</td> <td>根据实际情况采用%E或%F格式(以获得更简洁、准确的输出)</td> </tr> </tbody> </table> <p>示例代码如下:</p> <pre><code>f := 12.34 fmt.Printf(&amp;quot;%b\n&amp;quot;, f) fmt.Printf(&amp;quot;%e\n&amp;quot;, f) fmt.Printf(&amp;quot;%E\n&amp;quot;, f) fmt.Printf(&amp;quot;%f\n&amp;quot;, f) fmt.Printf(&amp;quot;%g\n&amp;quot;, f) fmt.Printf(&amp;quot;%G\n&amp;quot;, f)</code></pre> <p>输出结果如下:</p> <pre><code> 6946802425218990p-49 1.234000e+01 1.234000E+01 12.340000 12.34 12.34</code></pre> <h4>字符串和[]byte</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%s</td> <td>直接输出字符串或者[]byte</td> </tr> <tr> <td>%q</td> <td>该值对应的双引号括起来的go语法字符串字面值,必要时会采用安全的转义表示</td> </tr> <tr> <td>%x</td> <td>每个字节用两字符十六进制数表示(使用a-f</td> </tr> <tr> <td>%X</td> <td>每个字节用两字符十六进制数表示(使用A-F)</td> </tr> </tbody> </table> <p>示例代码如下:</p> <pre><code> s := &amp;quot;枯藤&amp;quot; fmt.Printf(&amp;quot;%s\n&amp;quot;, s) fmt.Printf(&amp;quot;%q\n&amp;quot;, s) fmt.Printf(&amp;quot;%x\n&amp;quot;, s) fmt.Printf(&amp;quot;%X\n&amp;quot;, s)</code></pre> <p>输出结果如下:</p> <pre><code> 枯藤 &amp;quot;枯藤&amp;quot; e69eafe897a4 E69EAFE897A4</code></pre> <h4>指针</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%p</td> <td>表示为十六进制,并加上前导的0x</td> </tr> </tbody> </table> <p>示例代码如下:</p> <pre><code>a := 18 fmt.Printf(&amp;quot;%p\n&amp;quot;, &amp;amp;a) fmt.Printf(&amp;quot;%#p\n&amp;quot;, &amp;amp;a)</code></pre> <p>输出结果如下:</p> <pre><code> 0xc000054058 c000054058</code></pre> <h4>宽度标识符</h4> <p>宽度通过一个紧跟在百分号后面的十进制数指定,如果未指定宽度,则表示值时除必需之外不作填充。精度通过(可选的)宽度后跟点号后跟的十进制数指定。如果未指定精度,会使用默认精度;如果点号后没有跟数字,表示精度为0。举例如下</p> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>%f</td> <td>默认宽度,默认精度</td> </tr> <tr> <td>%9f</td> <td>宽度9,默认精度</td> </tr> <tr> <td>%.2f</td> <td>默认宽度,精度2</td> </tr> <tr> <td>%9.2f</td> <td>宽度9,精度2</td> </tr> <tr> <td>%9.f</td> <td>宽度9,精度0</td> </tr> </tbody> </table> <p>示例代码如下:</p> <pre><code>n := 88.88 fmt.Printf(&amp;quot;%f\n&amp;quot;, n) fmt.Printf(&amp;quot;%9f\n&amp;quot;, n) fmt.Printf(&amp;quot;%.2f\n&amp;quot;, n) fmt.Printf(&amp;quot;%9.2f\n&amp;quot;, n) fmt.Printf(&amp;quot;%9.f\n&amp;quot;, n)</code></pre> <p>输出结果如下:</p> <pre><code> 88.880000 88.880000 88.88 88.88 89</code></pre> <h4>其他falg</h4> <table> <thead> <tr> <th>占位符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>’+’</td> <td>总是输出数值的正负号;对%q(%+q)会生成全部是ASCII字符的输出(通过转义);</td> </tr> <tr> <td>’ ‘</td> <td>对数值,正数前加空格而负数前加负号;对字符串采用%x或%X时(% x或% X)会给各打印的字节之间加空格</td> </tr> <tr> <td>’-’</td> <td>在输出右边填充空白而不是默认的左边(即从默认的右对齐切换为左对齐);</td> </tr> <tr> <td>’#’</td> <td>八进制数前加0(%#o),十六进制数前加0x(%#x)或0X(%#X),指针去掉前面的0x(%#p)对%q(%#q),对%U(%#U)会输出空格和单引号括起来的go字面值;</td> </tr> <tr> <td>‘0’</td> <td>使用0而不是空格填充,对于数值类型会把填充的0放在正负号后面;</td> </tr> </tbody> </table> <p>举个例子:</p> <pre><code>s := &amp;quot;枯藤&amp;quot; fmt.Printf(&amp;quot;%s\n&amp;quot;, s) fmt.Printf(&amp;quot;%5s\n&amp;quot;, s) fmt.Printf(&amp;quot;%-5s\n&amp;quot;, s) fmt.Printf(&amp;quot;%5.7s\n&amp;quot;, s) fmt.Printf(&amp;quot;%-5.7s\n&amp;quot;, s) fmt.Printf(&amp;quot;%5.2s\n&amp;quot;, s) fmt.Printf(&amp;quot;%05s\n&amp;quot;, s)</code></pre> <p>输出结果如下:</p> <pre><code> 枯藤 枯藤 枯藤 枯藤 枯藤 枯藤 000枯藤</code></pre> <h3>1.1.3. 获取输入</h3> <p>Go语言fmt包下有fmt.Scan、fmt.Scanf、fmt.Scanln三个函数,可以在程序运行过程中从标准输入获取用户的输入。</p> <h4>fmt.Scan</h4> <p>函数定签名如下:</p> <pre><code>func Scan(a ...interface{}) (n int, err error)</code></pre> <ul> <li>Scan从标准输入扫描文本,读取由空白符分隔的值保存到传递给本函数的参数中,换行符视为空白符。</li> <li>本函数返回成功扫描的数据个数和遇到的任何错误。如果读取的数据个数比提供的参数少,会返回一个错误报告原因。</li> </ul> <p>具体代码示例如下:</p> <pre><code>func main() { var ( name string age int married bool ) fmt.Scan(&amp;amp;name, &amp;amp;age, &amp;amp;married) fmt.Printf(&amp;quot;扫描结果 name:%s age:%d married:%t \n&amp;quot;, name, age, married) }</code></pre> <p>将上面的代码编译后在终端执行,在终端依次输入枯藤、18和false使用空格分隔。</p> <pre><code> $ ./scan_demo 枯藤 18 false 扫描结果 name:枯藤 age:18 married:false</code></pre> <p>fmt.Scan从标准输入中扫描用户输入的数据,将以空白符分隔的数据分别存入指定的参数。</p> <h4>fmt.Scanf</h4> <p>函数签名如下:</p> <pre><code>func Scanf(format string, a ...interface{}) (n int, err error)</code></pre> <ul> <li>Scanf从标准输入扫描文本,根据format参数指定的格式去读取由空白符分隔的值保存到传递给本函数的参数中。</li> <li>本函数返回成功扫描的数据个数和遇到的任何错误。</li> </ul> <p>代码示例如下:</p> <pre><code>func main() { var ( name string age int married bool ) fmt.Scanf(&amp;quot;1:%s 2:%d 3:%t&amp;quot;, &amp;amp;name, &amp;amp;age, &amp;amp;married) fmt.Printf(&amp;quot;扫描结果 name:%s age:%d married:%t \n&amp;quot;, name, age, married) }</code></pre> <p>将上面的代码编译后在终端执行,在终端按照指定的格式依次输入枯藤、18和false。</p> <pre><code> $ ./scan_demo 1:枯藤 2:18 3:false 扫描结果 name:枯藤 age:18 married:false</code></pre> <p>fmt.Scanf不同于fmt.Scan简单的以空格作为输入数据的分隔符,fmt.Scanf为输入数据指定了具体的输入内容格式,只有按照格式输入数据才会被扫描并存入对应变量。</p> <p>例如,我们还是按照上个示例中以空格分隔的方式输入,fmt.Scanf就不能正确扫描到输入的数据。</p> <pre><code> $ ./scan_demo 枯藤 18 false 扫描结果 name: age:0 married:false</code></pre> <h4>fmt.Scanln</h4> <p>函数签名如下:</p> <pre><code>func Scanln(a ...interface{}) (n int, err error)</code></pre> <ul> <li>Scanln类似Scan,它在遇到换行时才停止扫描。最后一个数据后面必须有换行或者到达结束位置。</li> <li>本函数返回成功扫描的数据个数和遇到的任何错误。</li> </ul> <p>具体代码示例如下:</p> <pre><code> func main() { var ( name string age int married bool ) fmt.Scanln(&amp;amp;name, &amp;amp;age, &amp;amp;married) fmt.Printf(&amp;quot;扫描结果 name:%s age:%d married:%t \n&amp;quot;, name, age, married) }</code></pre> <p>将上面的代码编译后在终端执行,在终端依次输入枯藤、18和false使用空格分隔。</p> <pre><code> $ ./scan_demo 枯藤 18 false 扫描结果 name:枯藤 age:18 married:false</code></pre> <p>fmt.Scanln遇到回车就结束扫描了,这个比较常用。 </p> <h4>bufio.NewReader</h4> <p>有时候我们想完整获取输入的内容,而输入的内容可能包含空格,这种情况下可以使用bufio包来实现。示例代码如下:</p> <pre><code>func bufioDemo() { reader := bufio.NewReader(os.Stdin) // 从标准输入生成读对象 fmt.Print(&amp;quot;请输入内容:&amp;quot;) text, _ := reader.ReadString('\n') // 读到换行 text = strings.TrimSpace(text) fmt.Printf(&amp;quot;%#v\n&amp;quot;, text) }</code></pre> <h4>Fscan系列</h4> <p>这几个函数功能分别类似于fmt.Scan、fmt.Scanf、fmt.Scanln三个函数,只不过它们不是从标准输入中读取数据而是从io.Reader中读取数据。</p> <pre><code>func Fscan(r io.Reader, a ...interface{}) (n int, err error) func Fscanln(r io.Reader, a ...interface{}) (n int, err error) func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)</code></pre> <h4>Sscan系列</h4> <p>这几个函数功能分别类似于fmt.Scan、fmt.Scanf、fmt.Scanln三个函数,只不过它们不是从标准输入中读取数据而是从指定字符串中读取数据。</p> <pre><code>func Sscan(str string, a ...interface{}) (n int, err error) func Sscanln(str string, a ...interface{}) (n int, err error) func Sscanf(str string, format string, a ...interface{}) (n int, err error)</code></pre>

页面列表

ITEM_HTML