在程序的任何阶段,我们可能都会需要输入/输出一些数据,以及通过输入/输出记录各种数据以进行程序调试,这种基本的输入/输出将有助于我们编写更好的代码。然而 fmt 包就很好的提供了标准输入输出,便于进行打印等,本文将针对 fmt 包进行讨论。
1、fmt 包
fmt 包实现了格式化的输入输出,这与 C 语言中的 printf 和 scanf 类似,它属于 Go 的内置包,在安装 Go 的时候会自动安装到系统中,位于 $GOROOT\src\pkg\fmt目录中,其中包括以下源代码:
doc.goerrors.goformat.goprint.goscan.go
复制代码
1.1 常用函数
该包主要实现了字符串格式支持的读写功能,常用的函数有:
上述函数长的都很相似,很容易混淆、用错,在这里介绍一些方法方便区分它们。
1.2 格式化占位符
fmt 包在进行输入、输出时,都可采用格式化占位符完成格式化的处理,支持的格式化占位符如下:
(对应代码:https://github.com/xcbeyond/golangLearning/blob/main/ch3/fmt/format/formatTest.go)
普通占位符
定义一个结构体 Person,并赋值:
type Person struct { Name string}var p = new(Person)p.Name = "xcbeyond"
复制代码
布尔占位符
整数占位符
复数(实部、虚部)占位符
字符串占位符
□:表示一个空格
指针占位符
2、输出
Go 语言的标准输出是指将输出内容直接输出到控制台上,根据不同的输出需求选择不同的函数,这些函数基本都位于 $GOROOT\src\fmt\print.go 中,各个函数具体使用如下。
2.1 fmt.Print
fmt.Print 使用默认格式输出,其函数定义如下:
// Print formats using the default formats for its operands and writes to standard output.// Spaces are added between operands when neither is a string.// It returns the number of bytes written and any write error encountered.func Print(a ...interface{}) (n int, err error) { return Fprint(os.Stdout, a...)}
复制代码
示例如下:
name := "xcbeyond"fmt.Print(name) // xcbeyond
复制代码
2.2 fmt.Printf
fmt.Printf 根据格式化占位符进行格式化,并标准输出,其函数定义如下:
// Printf formats according to a format specifier and writes to standard output.// It returns the number of bytes written and any write error encountered.func Printf(format string, a ...interface{}) (n int, err error) { return Fprintf(os.Stdout, format, a...)}
复制代码
示例如下:
name := "xcbeyond"fmt.Printf("%.5s", name) // xcbey
复制代码
2.3 fmt.Println
fmt.Println 使用默认格式输出,并换行,其函数定义如下:
// Println formats using the default formats for its operands and writes to standard output.// Spaces are always added between operands and a newline is appended.// It returns the number of bytes written and any write error encountered.func Println(a ...interface{}) (n int, err error) { return Fprintln(os.Stdout, a...)}
复制代码
示例如下:
name := "xcbeyond"fmt.Println(name) // xcbeyond 换行
复制代码
2.4 fmt.Sprint
fmt.Sprint使用默认格式返回结果字符串,其函数定义如下:
// Sprint formats using the default formats for its operands and returns the resulting string.// Spaces are added between operands when neither is a string.func Sprint(a ...interface{}) string { p := newPrinter() p.doPrint(a) s := string(p.buf) p.free() return s}
复制代码
示例如下:
name := "xcbeyond"retStr := fmt.Sprint(name)fmt.Print(retStr) // xcbeyond
复制代码
2.5 fmt.Sprintf
fmt.Sprintf 根据格式化占位符进行格式化,返回结果字符串,其函数定义如下:
// Sprintf formats according to a format specifier and returns the resulting string.func Sprintf(format string, a ...interface{}) string { p := newPrinter() p.doPrintf(format, a) s := string(p.buf) p.free() return s}
复制代码
示例如下:
name := "xcbeyond"retStr2 := fmt.Sprintf("%.5s", name)fmt.Print(retStr2) // xcbey
复制代码
2.6 fmt.Sprintln
fmt.Sprintln 使用默认格式,返回结果字符串,其函数定义如下:
// Sprintln formats using the default formats for its operands and returns the resulting string.// Spaces are always added between operands and a newline is appended.func Sprintln(a ...interface{}) string { p := newPrinter() p.doPrintln(a) s := string(p.buf) p.free() return s}
复制代码
示例如下:
name := "xcbeyond"retStr3 := fmt.Sprintln(name)fmt.Print(retStr3) // xcbeyond 换行
复制代码
2.7 fmt.Fprint
// Fprint formats using the default formats for its operands and writes to w.// Spaces are added between operands when neither is a string.// It returns the number of bytes written and any write error encountered.func Fprint(w io.Writer, a ...interface{}) (n int, err error) { p := newPrinter() p.doPrint(a) n, err = w.Write(p.buf) p.free() return}
复制代码
2.8 fmt.Fprintf
// Fprintf formats according to a format specifier and writes to w.// It returns the number of bytes written and any write error encountered.func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { p := newPrinter() p.doPrintf(format, a) n, err = w.Write(p.buf) p.free() return}
复制代码
2.9 fmt.Fprintln
// Fprintln formats using the default formats for its operands and writes to w.// Spaces are always added between operands and a newline is appended.// It returns the number of bytes written and any write error encountered.func Fprintln(w io.Writer, a ...interface{}) (n int, err error) { p := newPrinter() p.doPrintln(a) n, err = w.Write(p.buf) p.free() return}
复制代码
3、输入
3.1 fmt.Fscan
3.2 fmt.Fscanf
3.3 fmt.Fscanln
3.4 fmt.Scan
3.5 fmt.Scanf
3.6 fmt.Scanln
3.7 fmt.Sscan
3.8 fmt.Sscanf
3.9 fmt.Sscanln
参考资料:
https://golang.org/pkg/fmt/
https://golangdocs.com/fmt-package-golang
https://yourbasic.org/golang/fmt-printf-reference-cheat-sheet/
评论