Interpretation of Go language documentation: Detailed explanation of fmt.Printf function

PHPz
Release: 2023-11-04 14:07:49
Original
1284 people have browsed it

Interpretation of Go language documentation: Detailed explanation of fmt.Printf function

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.

1. The basic syntax of the fmt.Printf function

The basic syntax of the fmt.Printf function is as follows:

func Printf(format string, a ...interface{}) (n int, err error)
Copy after login
  • format: format string, used to specify The output format. It can contain ordinary characters and formatting verbs (such as %d, %s, %f, etc.) to display the value of a variable.
  • a ...interface{}: Variable parameter list, used to pass variables that need to be formatted for display.

The function returns two values, n represents the number of bytes output, and err represents possible errors.

2. Output the formatted string

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)
}
Copy after login

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.
Copy after login

3. Output other data types

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)
}
Copy after login

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.
Copy after login

4. Control the output width and alignment

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)
}
Copy after login

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
Copy after login

By adjusting the first format string, we can control the width and alignment of the output to make the output result more Beautiful.

5. Summary

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!