Golang-输入与输出


1. 输出

1.1 Print类

  • func Print(a ...any) (n int, err error)

普通输出,默认不带换行。

package main

import (
	"fmt"
)

func main() {
	const name, age = "Kim", 22
	fmt.Print(name, " is ", age, " years old.\n")

	// It is conventional not to worry about any
	// error returned by Print.

}
Output:

Kim is 22 years old.
  • func Printf(format string, a ...any) (n int, err error)

格式化输出,默认不带换行。

package main

import (
	"fmt"
)

func main() {
	const name, age = "Kim", 22
	fmt.Printf("%s is %d years old.\n", name, age)

	// It is conventional not to worry about any
	// error returned by Printf.

}
Output:

Kim is 22 years old.
  • func Println(a ...any) (n int, err error)

普通输出,带换行。

package main

import (
	"fmt"
)

func main() {
	const name, age = "Kim", 22
	fmt.Println(name, "is", age, "years old.")

	// It is conventional not to worry about any
	// error returned by Println.

}
Output:

Kim is 22 years old.
  • 总结:

此类是最普通的输出,记住如何格式化输出和默认带换行。

1.2 Sprint类

  • func Sprint(a ...any) string

将普通输出转化为字符串返回,默认不带换行。

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	const name, age = "Kim", 22
	s := fmt.Sprint(name, " is ", age, " years old.\n")

	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.

}
Output:

Kim is 22 years old.
  • func Sprintf(format string, a ...any) string

将格式化输出转化为字符串返回,默认不带换行。

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	const name, age = "Kim", 22
	s := fmt.Sprintf("%s is %d years old.\n", name, age)

	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.

}
Output:

Kim is 22 years old.
  • func Sprintln(a ...any) string

将普通输出转化为字符串返回,默认带换行。

package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	const name, age = "Kim", 22
	s := fmt.Sprintln(name, "is", age, "years old.")

	io.WriteString(os.Stdout, s) // Ignoring error for simplicity.

}
Output:

Kim is 22 years old.
  • 总结

此类是Print类的延伸,不直接输出,而是返回字符串。

1.3 Fprint类

  • func Fprint(w io.Writer, a ...any) (n int, err error)

将普通输出通过Writer输出,返回的是输出字节数和error。

package main

import (
	"fmt"
	"os"
)

func main() {
	const name, age = "Kim", 22
	n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.\n")

	// The n and err return values from Fprint are
	// those returned by the underlying io.Writer.
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fprint: %v\n", err)
	}
	fmt.Print(n, " bytes written.\n")

}
Output:

Kim is 22 years old.
21 bytes written.
  • func Fprintf(w io.Writer, format string, a ...any) (n int, err error)

将格式化输出通过Writer输出,返回的是输出字节数和error。

package main

import (
	"fmt"
	"os"
)

func main() {
	const name, age = "Kim", 22
	n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.\n", name, age)

	// The n and err return values from Fprintf are
	// those returned by the underlying io.Writer.
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fprintf: %v\n", err)
	}
	fmt.Printf("%d bytes written.\n", n)

}
Output:

Kim is 22 years old.
21 bytes written.
  • func Fprintln(w io.Writer, a ...any) (n int, err error)

将普通输出通过Writer输出,默认带换行,返回的是输出字节数和error。

package main

import (
	"fmt"
	"os"
)

func main() {
	const name, age = "Kim", 22
	n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")

	// The n and err return values from Fprintln are
	// those returned by the underlying io.Writer.
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
	}
	fmt.Println(n, "bytes written.")

}
Output:

Kim is 22 years old.
21 bytes written.
  • 总结

此类是Print类的延伸,不直接输出,通过Writer输出,似乎一般用不到。

2. 输入

2.1 Scan类

  • func Scan(a ...any) (n int, err error)

用于接受任意个输入,不同输入用空格分割,换行当作空格处理。

package main

import "fmt"

func main(){
	var a, b int
	n, err := fmt.Scan(&a, &b)
	if err != nil {
		fmt.Println(n, a, b)
	}
}
  • func Scanf(format string, a ...any) (n int, err error)

格式化接受输入,用空格分割,可以换行,输入中的换行必须和format中的换行匹配。

package main

import "fmt"

func main(){
	fomat := "%f / %d / %s"
	var a float32
	var b int
	var c string
	n, err := fmt.Scan(&a, &b, &s)
}
  • func Scanln(a ...any) (n int, err error)

Scna类似,但是只接收一行的输入,用空格分割。

  • 总结

最普通的接收输入。

2.2 Sscan类

与Scan类基本一致,只不过多了一个参数str String,代表从这个字符串中接收输入。

2.3 Fscan类

与Scan类基本一致,只不过多了一个参数r io.Reader,代表从这个Reader中接收输入。


文章作者: foursevenlove
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 foursevenlove !
 上一篇
Golang-Reader和Writer Golang-Reader和Writer
Go语言的io包提供了读写操作的接口,其中最常用的就是Reader和Writer接口。Reader和Writer接口可以用来从一个数据源读取数据,或者向一个目标写入数据,不同的实现方式可以适用于不同的数据源和目标。
2023-03-01
下一篇 
快速导航 快速导航
2023-02-28 foursevenlove
  目录