输入,输出的i/o方法对于每一门语言来说都是重点和基础。现在对fmt包内的各个函数以及占位符进行一个总结。
Print系列函数会将内容输出到系统的标准输出,区别在于Print函数直接输出内容,Printf函数支持格式化输出字符串,Println会在输出内容然后加一个换行符(这点和其它语言一样)
源码中定义如下:
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)
Fprint
Fprint中的F指的就是File,print就是打印,顾名思义,Fprint系列函数被定义为用io.Writer这个函数向文件中打印内容,由界面端输出到文件中
源码中定义如下:
func Fprint(w io.Writer, a ...interface{}) (n int, err error)
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
func Fprintln(w io.Writer, a ...interface{}) (n int, err error)
例子:
// 向标准输出写入内容
fmt.Fprintln(os.Stdout, "向标准输出写入内容")
Sprint
Sprint系列函数把传入的数据生成并返回一个字符串
源码中定义如下:
func Sprint(a ...interface{}) string
func Sprintf(format string, a ...interface{}) string
func Sprintln(a ...interface{}) string
Errorf
Errorf函数根据format参数生成格式化字符串并返回一个包含该字符串的错误
func Errorf(format string, a ...interface{}) error
//fmt.Errorf加一个%w占位符用来生成一个可以包裹Error
de1Wrapping Error
e := errors.New("原始错误e")
w := fmt.Errorf("Wrap了一个错误%w", e)
Scan
Scan从标准输入扫描文本,读取由空白符分割的值保存到传递给本函数的参数中,换行符视为空白符。即,使用此函数的输入不能进行换行。 且,本函数返回成功扫描的数据个数和遇到的任何错误。如果读取的数据个数比提供的参数少,会返回一个错误报告原因。
Scanf
输入语句一般使用Scanf函数进行输入::Scanf从标准输入扫描文本,根据format参数指定的格式取读取由空白符分隔的值保存到传递给本函数的参数中
本函数会返回成功扫描的数据个数和遇到的任何错误
Scanln
Scanln类,可以返回成功扫描的数据个数和遇到的任何错误,比Scan多了一个操作,遇到一个换行符就停止扫描,即:最后一个数据后面必须有换行或者到达结束位置
func Scan(a ...interface{}) (n int, err error)
func Scanf(format string, a ...interface{}) (n int, err error)
Fscan
scan是读取数据的函数,只是Fscan不是从标准输入中读取函数,而是从io.Reader.即:从输出流中读取数据
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)
Sscan
同样是读取数据,类似于fmt.Scan,fmt.Scanf,fmt.Scanln三个函数,刺系列函数是从指定字符串中读取数据,而不是标准输入
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
格式化占位符
在进行数据输出的时候,会出现各种占位符,这些占位符多且庞杂,现在对占位符根据类型进行一个划分,利于使用
占位符 | 说明 |
---|---|
%v | 值的默认格式表示 |
%+v | 类似于%v,但输出时会添加字段名 |
%#v | 值的Go语法表示 |
%T | 打印值的类型 |
%% | 百分号 |
占位符 | 说明 |
---|---|
%t | true或false |
占位符 | 说明 |
---|---|
%p | 表示为十六进制,并加上前导的ox |
占位符 | 说明 |
---|---|
%s | 直接输出字符串或者[]byte |
%q | 该值对应的双引号括起来的go语法字符串字面值 |
%x | 每个字节用两字符十六进制表示(使用a-f |
%X | 每个字节用两字符十六进制表示(使用A-F) |
占位符 | 说明 |
---|---|
%b | 表示为为二进制 |
%c | 改值对应的unicode码值 |
%d | 表示为十进制 |
%o | 表示为八进制 |
%x | 表示为十六进制,使用a-f |
%X | 表示为十六进制,使用A-F |
%U | 表示为Unicode格式:U+1234,等价于"U+%04X" |
%q | 该值对应的单引号括起来的go语法字符字面值,必要时会采用安全的转义表示 |
占位符 | 说明 |
---|---|
%b | 无小数部分,二进制指数的科学计数法,如-123456-78 |
%e | 科学计数法,如-123456e+78 |
%E | 科学计数法,如-1234.456E+78 |
%f | 有小数部分但无指数部分,如123.456 |
%F | 等价于%f |
%g | 根据实际情况采用%e或%f格式(以获得更简洁,准确的输出) |
%G | 根据实际情况采用%E或%f格式 (以获得更简洁,准确的输出) |
General:
%v the value in a default format
when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
%% a literal percent sign; consumes no value
Boolean:
%t the word true or false
Integer:
%b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%O base 8 with 0o prefix
%q a single-quoted character literal safely escaped with Go syntax.
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U+1234; same as "U+%04X"
Floating-point and complex constituents:
%b decimalless scientific notation with exponent a power of two,
in the manner of strconv.FormatFloat with the 'b' format,
e.g. -123456p-78
%e scientific notation, e.g. -1.234456e+78
%E scientific notation, e.g. -1.234456E+78
%f decimal point but no exponent, e.g. 123.456
%F synonym for %f
%g %e for large exponents, %f otherwise. Precision is discussed below.
%G %E for large exponents, %F otherwise
%x hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20
%X upper-case hexadecimal notation, e.g. -0X1.23ABCP+20
String and slice of bytes (treated equivalently with these verbs):
%s the uninterpreted bytes of the string or slice
%q a double-quoted string safely escaped with Go syntax
%x base 16, lower-case, two characters per byte
%X base 16, upper-case, two characters per byte
Slice:
%p address of 0th element in base 16 notation, with leading 0x
Pointer:
%p base 16 notation, with leading 0x
The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly as if it were an integer.
The default format for %v is:
bool: %t
int, int8 etc.: %d
uint, uint8 etc.: %d, %#x if printed with %#v
float32, complex64, etc: %g
string: %s
chan: %p
pointer: %p