Interpretation of Go language documentation: Detailed explanation of fmt.Printf function
In Go language, through the functions provided by the fmt package, we can perform formatted input and output. Among them, the Printf function is one of the commonly used functions, which can output content according to the specified format string and display it in the console.
In this article, we will explain in detail how to use the fmt.Printf function and provide specific code examples to help readers better understand and master the functions of this function.
The basic syntax of the fmt.Printf function is as follows:
func Printf(format string, a ...interface{}) (n int, err error)
The function returns two values, n represents the number of bytes output, and err represents possible errors.
Using the Printf function, we can directly output the formatted string in the console. The following is a simple sample code:
package main import "fmt" func main() { name := "Alice" age := 18 fmt.Printf("My name is %s and I am %d years old. ", name, age) }
In the above sample code, we used the two formatting verbs %d and %s. Among them, %d is used to format integer variables, and %s is used to format string variables.
Execute the above code, we will get the following output:
My name is Alice and I am 18 years old.
In addition to string and integer types, the Printf function can also format output Other common data types, such as floating point numbers, Boolean values, characters, etc. The following is a sample code:
package main import "fmt" func main() { pi := 3.14159 isGoFun := true luckyNumber := '7' fmt.Printf("The value of Pi is %.2f. ", pi) fmt.Printf("Is Go fun? %t ", isGoFun) fmt.Printf("My lucky number is %c. ", luckyNumber) }
In the above sample code, %.2f specifies the precision of floating point output to 2 decimal places, %t is used to format Boolean values, and %c is used to format character.
Execute the above code, we will get the following output:
The value of Pi is 3.14. Is Go fun? true My lucky number is 7.
Through the Printf function, we can also control the output width and alignment Alignment. The following is a sample code:
package main import "fmt" func main() { name := "Bob" score := 95 fmt.Printf("%-10s %3d ", "Name", "Score") fmt.Println("--------------------") fmt.Printf("%-10s %3d ", name, score) }
In the above sample code, %-10s and = indicate that when outputting strings and integers, their widths are 10 and 3 respectively. By adding a "-" sign in front of the width, we can achieve left-aligned output.
Execute the above code, we will get the following output:
Name Score -------------------- Bob 95
By adjusting the first format string, we can control the width and alignment of the output to make the output result more Beautiful.
Through the above introduction, we can see the importance and flexibility of the fmt.Printf function in the Go language. It can not only output simple strings and numbers, but also format and output various common data types, and can control the output by controlling the formatted string.
In actual development, mastering the use of fmt.Printf function can help us better process and debug the program. I hope this article will be helpful in understanding and mastering the use of this function.
The above is the detailed content of Interpretation of Go language documentation: Detailed explanation of fmt.Printf function. For more information, please follow other related articles on the PHP Chinese website!