go语言fmt包输入输出,占位符分类

本文详细介绍了Go语言fmt包中的Print系列、Fprint系列、Sprint和Errorf等函数,以及Scan、Scanf和Scanln的输入处理,同时涵盖了各类占位符的用法,帮助开发者高效进行I/O操作和格式化输出。
摘要由CSDN通过智能技术生成

输入,输出的i/o方法对于每一门语言来说都是重点和基础。现在对fmt包内的各个函数以及占位符进行一个总结。

Print

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打印值的类型
%%百分号
布尔型占位符
占位符说明
%ttrue或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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值