📜  Golang 中的 fmt.Printf()函数示例

📅  最后修改于: 2021-10-24 14:06:03             🧑  作者: Mango

在 Go 语言中, fmt包使用类似于 C 的 printf() 和 scanf()函数的函数来实现格式化的 I/O。 Go 语言中的fmt.Printf()函数根据格式说明符格式化并写入标准输出。而且,这个函数是在 fmt 包下定义的。在这里,您需要导入“fmt”包才能使用这些功能。

句法:

func Printf(format string, a ...interface{}) (n int, err error)

参数:此函数接受两个参数,如下所示:

  • 格式字符串:这包含一些字符串和一些动词。
  • a …interface{}:它包含指定的常量变量。

返回值:它返回写入的字节数和遇到的任何写入错误。

转换字符:

Conversion Characters Description
%b It is used to format base 2 numbers
%o It is used to format base 8 numbers
%O It is used to format base 8 numbers with 0o prefix
%d It is used to format base 10 numbers with lower-case letters for a-f
%x It is used to format base 16 numbers with upper-case letters for A-F
%X It is used to format base 16 numbers
%g It is used to format float values
%s It is used to format string values
%t It is used to format true or false values
%e It is used to format scientific values

示例 1:

Go
// Golang program to illustrate the usage of
// fmt.Printf() function
 
// Including the main package
package main
 
// Importing fmt
import (
    "fmt"
)
 
// Calling main
func main() {
 
    // Declaring some const variables
    const name, dept = "GeeksforGeeks", "CS"
 
    // Calling Printf() function
    fmt.Printf("%s is a %s Portal.\n", name, dept)
 
    // It is conventional not to worry about any
    // error returned by Printf.
 
}


Go
// Golang program to illustrate the usage of
// fmt.Printf() function
 
// Including the main package
package main
 
// Importing fmt
import (
    "fmt"
)
 
// Calling main
func main() {
 
    // Declaring some const variables
    const num1, num2, num3 = 5, 10, 15
 
    // Calling Printf() function
    fmt.Printf("%d + %d = %d\n", num1, num2, num3)
 
    // It is conventional not to worry about any
    // error returned by Printf.
 
}


Go
// Golang program to illustrate the usage of
// fmt.Printf() function
 
// Including the main package
package main
 
// Importing fmt
import (
    "fmt"
)
 
// Calling main
func main(){
 
    var str = "Geeksforgeeks"
      fmt.Printf("The string is %s \n", str)
    var num1 int = 21
      fmt.Printf("The decimal value is %d \n", num1)
    var num2 float32 = 7.786
      fmt.Printf("The floating point is %g \n", num2)
    var num3 int = 14
      fmt.Printf("The binary value of num3 is %b \n", num3)
    var num4 = 4 + 1i
    fmt.Printf("Scientific Notation of num4 : %e \n", num4)
}


输出:

GeeksforGeeks is a CS portal.

示例 2:

// Golang program to illustrate the usage of
// fmt.Printf() function
 
// Including the main package
package main
 
// Importing fmt
import (
    "fmt"
)
 
// Calling main
func main() {
 
    // Declaring some const variables
    const num1, num2, num3 = 5, 10, 15
 
    // Calling Printf() function
    fmt.Printf("%d + %d = %d\n", num1, num2, num3)
 
    // It is conventional not to worry about any
    // error returned by Printf.
 
}

输出:

5 + 10 = 15

示例 3:

// Golang program to illustrate the usage of
// fmt.Printf() function
 
// Including the main package
package main
 
// Importing fmt
import (
    "fmt"
)
 
// Calling main
func main(){
 
    var str = "Geeksforgeeks"
      fmt.Printf("The string is %s \n", str)
    var num1 int = 21
      fmt.Printf("The decimal value is %d \n", num1)
    var num2 float32 = 7.786
      fmt.Printf("The floating point is %g \n", num2)
    var num3 int = 14
      fmt.Printf("The binary value of num3 is %b \n", num3)
    var num4 = 4 + 1i
    fmt.Printf("Scientific Notation of num4 : %e \n", num4)
}

输出:

The string is Geeksforgeeks  
The decimal value is 21  
The floating point is 7.786  
The binary value of num3 is 1110  
Scientific Notation of num4 : (4.000000e+00+1.000000e+00i)