fmt.Printf() Function in Golang With Examples

In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Printf() function in Go language formats according to a format specifier and writes to standard output. Moreover, this function is defined under the fmt package. Here, you need to import the “fmt” package in order to use these functions.

Syntax:

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

Parameters: This function accepts two parameters which are illustrated below:

  • format string: This contains some strings along with some verbs.
  • a …interface{}: This contains specified constant variables.

Return Value: It returns the number of bytes written and any write error encountered.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.
  
}

chevron_right


Output:

GeeksforGeeks is a CS portal.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.
  
}

chevron_right


Output:

5 + 10 = 15



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.