23.1格式化输入
1.从终端(一般来说是键盘)获取用户的输入
1: fmt.Scanf 格式化的输入,空格作为分隔符2: fmt.Scan 从终端获取输入,存储在Scanln中的参数里,空格和换行符作为分隔符3: fmt.Scanln 同上,空格作为分隔符,遇到换行符结束
示例:
主要区分键盘输入过程中中途回车键和代码中
的问题
1: package main2:3: import "fmt"4:5: func testScanf() {6: var a int7: var b string8: var c float329: _, err := fmt.Scanf("%d %s %f", &a, &b, &c)10: if err != nil {11: fmt.Println("err=", err)12: }13: fmt.Printf("a=%d b=%s c=%f ", a, b, c)14: }15:16: func testScan() {17: var a int18: var b string19: var c float3220: _, err := fmt.Scan(&a, &b, &c)21: if err != nil {22: fmt.Println("err=", err)23: }24: fmt.Printf("a=%d b=%s c=%f ", a, b, c)25: }26:27: func testScanln() {28: var a int29: var b string30: var c float3231: _, err := fmt.Scanln(&a, &b, &c)32: if err != nil {33: fmt.Println("err=", err)34: }35: fmt.Printf("a=%d b=%s c=%f ", a, b, c)36: }37:38: func main(){39: testScanf()40: testScan()41: testScanln()42: }
2.从字符串中获取输入,与终端输入类似
1: fmt.Sscanf()2: fmt.Sscan()3: fmt.Sscanln()
代码示例:
1: package main2:3: import "fmt"4:5: func testSscanf() {6: var a int7: var b string8: var c float329: str := "88 hello 8.8"10: _, err := fmt.Sscanf(str, "%d %s %f", &a, &b, &c)11: if err != nil {12: fmt.Println("err=", err)13: }14: fmt.Printf("a=%d, b=%s, c=%f ", a, b, c)15: }16:17: func testSscan() {18: var a int19: var b string20: var c float3221: str := "88 hello 8.8"22: _, err := fmt.Sscan(str, &a, &b, &c)23: if err != nil {24: fmt.Println("err=", err)25: }26: fmt.Printf("a=%d, b=%s, c=%f ", a, b, c)27: }28:29: func testSscanln() {30: var a int31: var b string32: var c float3233: str := "88 hello 8.8"34: _, err := fmt.Sscanln(str, &a, &b, &c)35: if err != nil {36: fmt.Println("err=", err)37: }38: fmt.Printf("a=%d, b=%s, c=%f ", a, b, c)39: }40:41:42: func main() {43: testSscanf()44: testSscan()45: testSscanln()46: }47:
1: >>> a=88, b=hello, c=8.8000002: a=88, b=hello, c=8.8000003: err= unexpected newline4: a=88, b=, c=0.000000
3.从文件中获取格式化输入
代码示例
1: func main() {2: var a int3: var b string4: var c float325: _, err1 := fmt.Fscanf(os.Stdin, "%d%s%f", &a, &b, &c)6: if err1 != nil {7: fmt.Println("err1=", err1)8: }9: fmt.Println(a, b, c)10: _, err2 := fmt.Fscan(os.Stdin, &a, &b, &c)11: // 每输入一个回车一下发现可以继续回车12: if err2 != nil {13: fmt.Println("err2=", err2)14: }15: fmt.Println(a, b, c)18: fmt.Println("err3=", err3)16: _, err3 := fmt.Fscanln(os.Stdin, &a, &b, &c)17: if err3 != nil {19: }20: fmt.Println(a, b, c)21: }
23.2格式化输出
打印在终端上
1: fmt.Printf() format string2: fmt.Println() 自带换行3: fmt.Print() 不会换行
格式化输出字符串(返回字符串)
1: fmt.Sprintf() format string2: fmt.Sprintln() 按空格进行格式化并换行3: fmt.Sprint() 不会换行
格式化输出到文件中
1: fmt.Fprintf() format string 写入到文件2: fmt.Fprintln() 把单个或多个文件写入到文件中,并换行3: fmt.Fprint() 不换行
示例
1: func main() {2: var a int3: var b string4: var c float325: fmt.Fscanf(os.Stdin, "%d%s%f", &a, &b, &c)6: fmt.Fprintln(os.Stdout, "stdout:", a, b, c)7: }
终端输入输出背后的原理
终端其实是一个文件
1: os.Stdin 标准输入的文件示例,*File2: os.Stdout 标准输出的文件示例,*File3: os.Stderr 标准错误输出的文件实例 *File
终端读取
1: File.Read(b []byte)2: // os.Stdin.Read()
终端输出
1: File.Write()2: File.WriteString(str string)
带缓冲区的读写
相对内存读写来说,文件(磁盘)的性能其实比较差,go语言中使用bufio包来实现带缓冲的读写。
代码示例
1: var inputReader *bufio.Reader2: func main() {3: var input string4: var err error5: inputReader = bufio.NewReader(os.Stdin)6: fmt.Println("Please input:")7: //8: input, err = inputReader.ReadString(' ')9: if err == nil {10: fmt.Printf("input:%s ", input)11: } else {12: fmt.Println("err=", err)13: }14: }