参考https://studygolang.com/pkgdoc

导入方式:

import "fmt"

mt包实现了类似C语言printf和scanf的格式化I/O。格式化动作('verb')源自C语言但更简单。

func Printf

func Printf(format string, a ...interface{}) (n int, err error)

Printf根据format参数生成格式化的字符串并写入标准输出os.stdout。返回写入的字节数和遇到的任何错误。

func Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf根据format参数生成格式化的字符串并写入w。返回写入的字节数和遇到的任何错误。

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf根据format参数生成格式化的字符串并返回该字符串。

func Print

func Print(a ...interface{}) (n int, err error)

Print采用默认格式将其参数格式化并写入标准输出。如果两个相邻的参数都不是字符串,会在它们的输出之间添加空格。返回写入的字节数和遇到的任何错误。

func Fprint

func Fprint(w io.Writer, a ...interface{}) (n int, err error)

Fprint采用默认格式将其参数格式化并写入w。如果两个相邻的参数都不是字符串,会在它们的输出之间添加空格。返回写入的字节数和遇到的任何错误。

func Sprint

func Sprint(a ...interface{}) string

Sprint采用默认格式将其参数格式化,串联所有输出生成并返回一个字符串。如果两个相邻的参数都不是字符串,会在它们的输出之间添加空格。

func Println

func Println(a ...interface{}) (n int, err error)

Println采用默认格式将其参数格式化并写入标准输出。总是会在相邻参数的输出之间添加空格并在输出结束后添加换行符。返回写入的字节数和遇到的任何错误。

func Fprintln

func Fprintln(w io.Writer, a ...interface{}) (n int, err error)

Fprintln采用默认格式将其参数格式化并写入w。总是会在相邻参数的输出之间添加空格并在输出结束后添加换行符。返回写入的字节数和遇到的任何错误。

func Sprintln

func Sprintln(a ...interface{}) string

Sprintln采用默认格式将其参数格式化,串联所有输出生成并返回一个字符串。总是会在相邻参数的输出之间添加空格并在输出结束后添加换行符。

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf根据format参数生成格式化字符串并返回一个包含该字符串的错误。

举例:

package main
import("fmt""os""log""bufio"
)
func main() {fmt.Printf("right now is try to test %s, this is the %dst time to test fmt\n", "Printf", 1)writer := bufio.NewWriter(os.Stdout)fmt.Fprintf(writer, "right now is try to test %s, this is the %dst time to test fmt\n", "Fprintf", 2)//等价于直接使用Printf,因为指定将值输出到标准输出
    writer.Flush()file, err := os.Create("testFmt.txt")if err != nil {log.Fatal(err)}defer file.Close()writer1 := bufio.NewWriter(file)_, err = fmt.Fprintf(writer1, "right now is try to test %s, this is the %dst time to test fmt\n", "Fprintf", 3)if err != nil {log.Fatal(err)}prompt := "right now is try to test %s, this is the %dst time to test fmt"prompt = fmt.Sprintf(prompt, "Sprintf", 4)//串联输入的字符串参数,如果并不是都是字符串,则用空格将其分隔
    fmt.Println(prompt)fmt.Print(prompt, "\n") //两者的区别在于Println会自动换行,而Print不会//串联所有输出生成并返回一个字符串。如果两个相邻的参数都不是字符串,会在它们的输出之间添加空格prompt1 := fmt.Sprint("right now is try to test", "Sprint", ", this is the ", 5, "st time to test fmt")fmt.Println(prompt1)fmt.Fprintln(writer1, "right now is try to test", "Fprintln", ", this is the ", 6, "st time to test fmt")writer1.Flush()prompt2 := fmt.Sprintln("right now is try to test", "Sprintln", ", this is the ", 7, "st time to test fmt")fmt.Println(prompt2)err = fmt.Errorf("test how to use %s to generate an err info", "Errorf")if err != nil {log.Fatal(err)}}

返回:

bogon:go-learning user$ go run test.go
right now is try to test Printf, this is the 1st time to test fmt
right now is try to test Fprintf, this is the 2st time to test fmt
right now is try to test Sprintf, this is the 4st time to test fmt
right now is try to test Sprintf, this is the 4st time to test fmt
right now is try to testSprint, this is the 5st time to test fmt
right now is try to test Sprintln , this is the  7 st time to test fmt2019/01/27 12:34:01 test how to use Errorf to generate an err info
exit status 1

然后对应的testFmt.txt中的输出是:

right now is try to test Fprintf, this is the 3st time to test fmt
right now is try to test Fprintln , this is the  6 st time to test fmt

Scanning

一系列类似的函数可以扫描格式化文本以生成值。

Scan、Scanf和Scanln从标准输入os.Stdin读取文本;Fscan、Fscanf、Fscanln从指定的io.Reader接口读取文本;Sscan、Sscanf、Sscanln从一个参数字符串读取文本。

Scanln、Fscanln、Sscanln会在读取到换行时停止,并要求一次提供一行所有条目;Scanf、Fscanf、Sscanf只有在格式化文本末端有换行时会读取到换行为止;其他函数会将换行视为空白。

Scanf、Fscanf、Sscanf会根据格式字符串解析参数,类似Printf。例如%x会读取一个十六进制的整数,%v会按对应值的默认格式读取。格式规则类似Printf,有如下区别:

%p 未实现
%T 未实现
%e %E %f %F %g %G 效果相同,用于读取浮点数或复数类型
%s %v 用在字符串时会读取空白分隔的一个片段
flag '#'和'+' 未实现   

在无格式化verb或verb %v下扫描整数时会接受常用的进制设置前缀0(八进制)和0x(十六进制)。

宽度会在输入文本中被使用(%5s表示最多读取5个rune来生成一个字符串),但没有使用精度的语法(没有%5.2f,只有%5f)。

当使用格式字符串进行扫描时,多个连续的空白字符(除了换行符)在输出和输出中都被等价于一个空白符。在此前提下,格式字符串中的文本必须匹配输入的文本;如果不匹配扫描会中止,函数的整数返回值说明已经扫描并填写的参数个数。

在所有的扫描函数里,\r\n都被视为\n。

在所有的扫描函数里,如果一个操作数实现了Scan方法(或者说,它实现了Scanner接口),将会使用该接口为该操作数扫描文本。另外,如果如果扫描到(准备填写)的参数比提供的参数个数少,会返回一个错误。

提供的所有参数必须为指针或者实现了Scanner接口。注意:Fscan等函数可能会在返回前多读取一个rune,这导致多次调用这些函数时可能会跳过部分输入。只有在输入里各值之间没有空白时,会出现问题。如果提供给Fscan等函数的io.Reader接口实现了ReadRune方法,将使用该方法读取字符。如果该io.Reader接口还实现了UnreadRune方法,将是使用该方法保存字符,这样可以使成功执行的Fscan等函数不会丢失数据。

如果要给一个没有这两个方法的io.Reader接口提供这两个方法,使用bufio.NewReader。

func Scan

func Scan(a ...interface{}) (n int, err error)

Scan从标准输入扫描文本,将成功读取的空白分隔的值保存进成功传递给本函数的参数。换行视为空白。返回成功扫描的条目个数和遇到的任何错误。如果读取的条目比提供的参数少,会返回一个错误报告原因。

举例:

package main
import("fmt""log"
)
func main() {fmt.Println("enter two parameter : ")var param1, param2 stringn, err := fmt.Scan(&param1, &param2)if err != nil{log.Fatal(err)}fmt.Println(param1, param2, n)
}

返回:

bogon:go-learning user$ go run test.go
enter two parameter :
test scan
test scan 2

func Scanln

func Scanln(a ...interface{}) (n int, err error)

Scanln类似Scan,但会在换行时才停止扫描。最后一个条目后必须有换行或者到达结束位置。

举例:

package main
import("fmt""log"
)
func main() {fmt.Println("enter two parameter : ")var param1, param2 stringn, err := fmt.Scanln(&param1, &param2)if err != nil{log.Fatal(err)}fmt.Println(param1, param2, n)
}

返回:

bogon:go-learning user$ go run test.go
enter two parameter :
test scanln
test scanln 2

func Scanf

func Scanf(format string, a ...interface{}) (n int, err error)

Scanf从标准输入扫描文本,根据format 参数指定的格式将成功读取的空白分隔的值保存进成功传递给本函数的参数。返回成功扫描的条目个数和遇到的任何错误。

举例:

package main
import("fmt"// "os"// "bufio""log"
)
func main() {fmt.Println("enter two float parameter : ")var param1, param2 float64// n, err := fmt.Scanf("%.2f %.2f", &param1, &param2)//会返回错误 2019/01/27 14:15:20 bad verb '%.' for float32,因为scan不支持精度// n, err := fmt.Scanf("%f %f", &param1, &param2)//只能设置宽度,精度默认为2,%5s表示最多读取5个rune来生成一个字符串//返回// enter two float parameter : // 3.456 2345.6// 3.45 2345.6 2n, err := fmt.Scanf("%4f %7f", &param1, &param2)//只能设置宽度,%4f表示你的输入包括小数点只能有4位,多余部分被舍弃或被下一个格式化获取if err != nil{//如果第一个输入值大于4位,那么就会报错 expected space in input to match format,因为下一个得到的不是空格,而是一个多余数值
        log.Fatal(err)}// 返回:// enter two float parameter : // 3.45 2345.6// 3.45 2345.6 2
    fmt.Println(param1, param2, n)
}

func Fscan

func Fscan(r io.Reader, a ...interface{}) (n int, err error)

Fscan从r扫描文本,将成功读取的空白分隔的值保存进成功传递给本函数的参数。换行视为空白。返回成功扫描的条目个数和遇到的任何错误。如果读取的条目比提供的参数少,会返回一个错误报告原因。

举例:

package main
import("fmt""os""bufio""log"
)
func main() {file, err :=os.Open("testFscan.txt")if err != nil{log.Fatal(err)}   defer file.Close()reader := bufio.NewReader(file)fmt.Println("read the content of testFscan.txt: ")var param1, param2 stringn, err := fmt.Fscan(reader, &param1, &param2)if err != nil{log.Fatal(err)}fmt.Println(param1, param2, n)
}

testFscan.txt的内容是:

Right now is trying to test Fscan
This is the end!!

返回:

userdeMBP:go-learning user$ go run test.go
read the content of testFscan.txt:
Right now 2

因为他是以空格为分隔的

func Fscanln

func Fscanln(r io.Reader, a ...interface{}) (n int, err error)

Fscanln类似Fscan,但会在换行时才停止扫描。最后一个条目后必须有换行或者到达结束位置。

举例:

package main
import("fmt""strings""log""io"
)
func main() {str := `Fscanln 1 34.5677 test 2 3243.53`reader := strings.NewReader(str)var param1 stringvar param2 intvar param3 float64for{n, err := fmt.Fscanln(reader, &param1, &param2, &param3)if err != nil {if err == io.EOF {break}log.Fatal(err)}fmt.Printf("%d : %s, %d, %f\n", n, param1, param2, param3)}
}

返回:

userdeMBP:go-learning user$ go run test.go
3 : Fscanln, 1, 34.567700
3 : test, 2, 3243.530000

func Fscanf

func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)

Fscanf从r扫描文本,根据format 参数指定的格式将成功读取的空白分隔的值保存进成功传递给本函数的参数。返回成功扫描的条目个数和遇到的任何错误。

举例:

package main
import("fmt""strings""log"
)
func main() {// str := `Fscanln 1 34.5677  //不能使用这种书写方式,因为遇见换行符会报错 unexpected newline// test 2 3243.53`str := "Fscanln 1 34.5677"reader := strings.NewReader(str)var(param1 stringparam2 intparam3 float64)n, err := fmt.Fscanf(reader, "%s %d %f", &param1, &param2, &param3)if err != nil {log.Fatal(err)}fmt.Println( n, param1, param2, param3)}

返回:

userdeMBP:go-learning user$ go run test.go
3 Fscanln 1 34.5677

func Sscan

func Sscan(str string, a ...interface{}) (n int, err error)

Sscan从字符串str扫描文本,将成功读取的空白分隔的值保存进成功传递给本函数的参数。换行视为空白。返回成功扫描的条目个数和遇到的任何错误。如果读取的条目比提供的参数少,会返回一个错误报告原因。

举例:

package main
import("fmt""log"
)
func main() {var(param1 stringparam2 intparam3 float64)n, err := fmt.Sscan("Fscanln 1 34.5677", &param1, &param2, &param3)if err != nil {log.Fatal(err)}fmt.Println( n, param1, param2, param3)//返回 3 Fscanln 1 34.5677

}

func Sscanln

func Sscanln(str string, a ...interface{}) (n int, err error)

Sscanln类似Sscan,但会在换行时才停止扫描。最后一个条目后必须有换行或者到达结束位置。

和Sscan相似,就不举例了,注意:

作为Sscan和Sscanln的string参数的值不能是:

    str := `Fscanln 1 34.5677test 2 3243.53`

因为他不是reader,没有指针指向上次读取的地方,因此每一次读取都会从string的头开始,然后读到换行符就会停止,这样就不会读取到第二行 test 2 3243.53 的值

func Sscanf

func Sscanf(str string, format string, a ...interface{}) (n int, err error)

Sscanf从字符串str扫描文本,根据format 参数指定的格式将成功读取的空白分隔的值保存进成功传递给本函数的参数。返回成功扫描的条目个数和遇到的任何错误。

举例:

package main
import("fmt""log"
)
func main() {var(param1 stringparam2 intparam3 float64)n, err := fmt.Sscanf("Fscanln 1 34.5677", "%s %d %f", &param1, &param2, &param3)if err != nil {log.Fatal(err)}fmt.Println( n, param1, param2, param3)}

返回:

userdeMBP:go-learning user$ go run test.go
3 Fscanln 1 34.5677

转载于:https://www.cnblogs.com/wanghui-garcia/p/10326395.html

go标准库的学习-fmt相关推荐

  1. go标准库的学习-crypto/sha1

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha1" sha1包实现了SHA1哈希算法,参见RFC 3174. ...

  2. 《C++标准库》学习笔记 — STL —流

    <C++标准库>学习笔记 - STL -流 一.操控器 1.原理 2.自定义操控器 3.控制输入的宽度 二.自定义 I/O 操作符 1.重载输出操作符 2.输入操作符 三.自定义格式化标志 ...

  3. Golang标准库中的fmt

    Golang标准库中的fmt fmt包实现了类似C语言printf和scanf的格式化I/O.主要分为向外输出内容和获取输入内容两大部分. 1. 向外输出 标准库fmt提供了以下几种输出相关函数. P ...

  4. C++“准”标准库Boost学习指南(1):智能指针Boost.smart_ptr

    我们学习C++都知道智能指针,例如STL中的std::auto_ptr,但是为什么要使用智能指针,使用它能带给我们什么好处呢? 最简单的使用智能指针可以不会因为忘记delete指针而造成内存泄露.还有 ...

  5. python标准库之socket_python标准库SocketServer学习

    导语:大牛们常常说阅读源码是很低效的学习方法.但对我辈初学者而言,阅读源码却是掌握编程思想.编码规范的好途径.简而言之,读源码不是万能的,不读源码是万万不能的. SocketServer是标准库中一个 ...

  6. go标准库的学习-crypto/aes

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/aes" aes包实现了AES加密算法,参见U.S. Federal ...

  7. go标准库的学习-sync互斥

    https://studygolang.com/pkgdoc 导入方法: import "sync" sync包提供了基本的同步基元,如互斥锁.除了Once和WaitGroup类型 ...

  8. go标准库的学习-time

    参考https://studygolang.com/pkgdoc 导入形式: import "time" time包提供了时间的显示和测量用的函数.日历的计算采用的是公历. 1&g ...

  9. go标准库的学习-errors

    参考https://studygolang.com/pkgdoc 导入方式: import "errors" errors包实现了创建错误值的函数. 1)func New func ...

最新文章

  1. swift(一)基础变量类型
  2. Android 打印log 在logcat 看不到
  3. LVS (一) 原理
  4. 推荐一些好的linux学习网站
  5. RabbitMQ之mandatory和immediate
  6. 新挑战、新架构下的数据保护新需求
  7. 使用threading模块实现多线程
  8. 可以让你少奋斗十年的工作经验(转)
  9. python 清空文件_python:文件的读取、创建、追加、删除、清空
  10. 天猫魔盒1代TMB100E刷机, 以及右声道无声的问题
  11. 罗技G29方向盘与Unity的连接交互
  12. 计算机考研复试问题汇总(408+计算机前言知识)
  13. 附资料:工程总承包项目管理流程图(全套)
  14. ioncube php encode,ionCube PHP解密
  15. mysqldump关于--set-gtid-purged=OFF的使用(好文章!!)
  16. php解密方法,php加密解密的几种方法的使用教程
  17. Libgdx Box2D实战---放开那小球(二:Box2D介绍)
  18. 推荐模型-上下文感知-2016:FNN模型【FM家族】【FM+MLP=FNN】
  19. Shell脚本自动源码包安装LA/NMP架构详解(赠软件包+脚本)
  20. (区块链溯源)基于Hyperledger Fabric 区块链的产品溯源( 化妆品 )

热门文章

  1. python基本使用-Python的基本用法
  2. python导入处理txt文件-python读取大文件踩过的坑——读取txt文件词向量
  3. 学python找工作好找吗-前辈经历告诉你Python新手好找工作吗?初级岗位多不多
  4. python项目实例代码-python开源项目及示例代码
  5. python对excel某一列去重-python中怎么对dataframe列去重
  6. python学精通要多久-python多久能精通
  7. python基础指令-python的一些基本命令
  8. python怎么安装模块-Python模块及其导入
  9. python编程输入标准-Python练习题,,T1.编写程序,输入
  10. 进入docker容器之后,找不到rosbag命令(ros已经安装)