VOOZH about

URL: https://www.geeksforgeeks.org/go-language/fmt-printf-function-in-golang-with-examples/

⇱ fmt.Printf() Function in Golang With Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

fmt.Printf() Function in Golang With Examples

Last Updated : 28 Apr, 2025

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.

Conversion Characters:

Conversion CharactersDescription
%bIt is used to format base 2 numbers
%oIt is used to format base 8 numbers
%OIt is used to format base 8 numbers with 0o prefix
%dIt is used to format base 10 numbers with lower-case letters for a-f
%xIt is used to format base 16 numbers with upper-case letters for A-F
%XIt is used to format base 16 numbers
%gIt is used to format float values
%sIt is used to format string values
%tIt is used to format true or false values
%eIt is used to format scientific values

Example 1:

Output: 

GeeksforGeeks is a CS portal.


Example 2:

Output: 

5 + 10 = 15

Example 3:

Output:

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)
Comment
Article Tags:

Explore