Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is fmt.Printf in Go lang?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The fmt.Printf function in the GO programming language is a function used to print out a formatted string to the console. fmt.Printf supports custom format specifiers and uses a format string to generate the final output string.

Printf is the actual function, while fmt is the GO package that stores the definition of this function. So to use this function, you must import the fmt package in your file and access the Printf function within using the . notation: fmt.Printf

Function definition

The definition of the Printf function inside the fmt package is as follows:

Parameters

fmt.Printf first takes the format string as an argument followed by a variable number of arguments.

  • format: This argument is of type string and represents the string containing custom specifiers that the printf uses to format the final output string

  • a ...interface{}: This argument refers to the list of a variable number of arguments that need to be formatted and printed.

Return values

The fmt.Printf function can return two things:

  • count: The number of bytes that were written to the standard output.

  • err: Any error thrown during the execution of the function.

Common specifiers

Following is a table of the most commonly used format specifiers in GO and their descriptions:

Specifiers Description
%s To print a string
%d To print an integer
%v To print values of all elements in a structure
%+v To print the names and values of all elements in a structure

Example

The following example is a simple program where we print out a single string along with an integer value:

package main
import (
"fmt"
)
func main() {
// declaring variables of different datatypes
var message string = "Hello and welcome to"
var year int = 2021
// printing out the declared variables following the format string
fmt.Printf("%s educative in %d",message, year)
}

This example shows you how you can make use of the return values from the fmt.Printf function:

package main
import (
"fmt"
)
func main() {
// declaring variables of different datatypes
var message string = "Hello and welcome to"
var year int = 2021
// storing the return values of Printf
char_count, error := fmt.Printf("%s Educative in %d\n",message, year)
if error == nil {
fmt.Printf("Printf executed without errors!!!\nNumber of characters printed: %d",char_count )
}
}

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring