“This is the 14th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

fmt

FMT is a formatted input-output library in GO language, which is mainly divided into two parts, namely the output part and the input part.

The output

Print

The main function of Print is output. The functions similar to Print are as follows:

  • func Print(a ... interface{}) (n int, err error): Direct output
  • func Println(a ... interface{}) (n int, err error)Println: uses the same method as Print, but the difference is that Println wraps automatically after each Print. If Print is used to wrap a line, it needs to wrap at the end\n.
  • func Printf(format string, a ... interface{}) (n int, err error): supports formatting output strings. When using Printf, you can use formatting string placeholders in functions.

Code examples:

func main(a) {
	a := "Lee"
	fmt.Print("Hello, everyone,")
	fmt.Printf("I am: %s \n",a)
	fmt.Println("I'm learning Go.")}Copy the code

Running results:

Common placeholders:

  • %d: integer decimal placeholder
  • %b: integer binary placeholder
  • % O: integer octal placeholder
  • %x: integer hexadecimal placeholder, where a to f are lowercase
  • %X: integer hexadecimal placeholder, where A to F are uppercase
  • %s: string placeholder
  • %f: floating point placeholder
  • %e: scientific floating-point number: for example, 1.234e+10
  • %E: scientific floating-point number: for example, 1.234E+10
  • %t: Boolean type placeholder
  • %p: pointer, in hexadecimal notation, prefixed with 0x

Fprint

The Fprint family of functions also has the following three functions. This function outputs content to a variable w of the IO.Writer interface type, which is usually used to write data to a file.

  • 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)

Code examples:

func main(a) {
	file, err := os.OpenFile("D://test/a.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	iferr ! =nil {
		fmt.Println("Error opening file :", err)
		return
	}
	name := "lee"
	age := 23
	// Write data to a file
	fmt.Fprintf(file, "Name: % S, Age: %d \n", name, age)
	// Write data to the console
	fmt.Fprintf(os.Stdout, "Write succeeded")}Copy the code

Running results:

The above code successfully writes to the A.txt file.

Sprint

Sprint formats the incoming content into a string and returns it. Similar to Sprint, there are several functions:

  • func Sprint(a ... interface{}) string: Direct return
  • func Sprintf(format string, a ... interface{}) string: formatting returns
  • func Sprintln(a ... interface{}) string: Wrap back

Code examples:

func main(a) {
	a := "Lee"
	s1 := fmt.Sprint("Straight back")
	s2 := fmt.Sprintln("Wrap back")
	s3 := fmt.Sprintf("Format returns: %s", a)
	fmt.Print(s1)
	fmt.Print(s2)
	fmt.Print(s3)
}
Copy the code

Running results:

Errorf

Errorf returns an error containing the string, formatted according to the incoming content.

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

Code examples:

func main(a) {
	a := "error"
	error := fmt.Errorf("Error: %s", a)
	fmt.Print(error)
}
Copy the code

Running results:

The input

The following functions can be used to get console input in the GO language:

func Scan(a ...interface{}) (n int, err error)
func Scanf(format string, a ...interface{}) (n int, err error)
func Scanln(a ...interface{}) (n int, err error) 
Copy the code

Scan

Scan reads text from standard input, separates it with a space character or a newline character, and assigns it to the parameters of the function. The function returns the number of successfully read data and the errors encountered.

Code examples:

func main(a) {
	var name string
	var age int
	fmt.Print("Please enter name and age:")
	fmt.Scan(&name, &age)
	fmt.Printf("Name: % S, Age: %d \n", name, age)
}
Copy the code

Running results:

Scanf

Scanf reads whitespace delimited values in the format specified by the format argument.

Code examples:

func main(a) {
	var name string
	var age int
	fmt.Print("Please enter name and age:")
	fmt.Scanf("name=%s age=%d", &name, &age)
	fmt.Printf("Name: % S, Age: %d \n", name, age)
}
Copy the code

Running results:

When Scanf is used for input, the format specified in the console input function must be complete. For example, the format specified in the above code is name=%s age=% D. This format must be used for input. If only Lee 23 is entered as in the previous Scan, the content cannot be read.

Scanln

Scanln is similar to Scan, except that Scanln stops reading as soon as a line break is entered.

Code examples:

func main(a) {
	var name string
	var age int
	fmt.Print("Please enter name and age:")
	fmt.Scanln(&name, &age)
	fmt.Printf("Name: % S, Age: %d \n", name, age)
}
Copy the code

Running results:

For example, in the above input, if only the name is entered and the line is wrapped, the reading will be stopped. However, in Scan, if the name is entered and the line is not stopped, the reading will be stopped after the age is entered.