Go Programming Println vs Printf vs Print

Printing out messages to the terminal can be used to debug code, show computed results, display messages, or even show warnings to the user of the program.

Go Programming Println vs Printf vs Print
Photo by Chinmay Bhattar / Unsplash

Printing out a series of outputs or strings to the terminal is crucial in any programming language. Printing out messages to the terminal can be used to debug code, show computed results, display messages, or even show warnings to the user of the program.

In the go programming language printing out things to the terminal can be achieved using a package known as fmt . In order to use it you are required to import the package.

The above command is a simple implementation of fmt to display a message to the terminal using go.

package main
import "fmt"

func main(){
fmt.Println("Welcome to Blue Doa Digest")
}
sample print

In this article, we will cover in detail the different ways you can print out messages to the terminal in the go programming language and their notable difference.

Print

The print is used to simply display string and does not format string in any way. You cannot embed a variable to be displayed with the string. Print in fmt is a simple implementation of printing things to the terminal.

package main
import "fmt"

func main(){
fmt.Print("Welcome to Blue Doa Digest")
}
print

Printf

The fmt Printf is an abbreviation of "Print Formatter" unlike Print , Printf is an advanced implementation of Print. Printf is able to format go data types to the string.

package main
import "fmt"

func main(){
publication:= "Blue doa digest"
fmt.Printf("Welcome to %s\n",publication)
}
Printf

In order to add a new line using printf we use \n to indicate a new line.

Println

The  go fmt println is an abbreviation of "Print Line".  It is similar to printf however println  fixes the issue of a new line by automatically adding a new line at the end of each output.

package main
import "fmt"

func main(){
name := "Alexander"
publication:= "Blue Doa Digest"
fmt.Println("Welcome",name,"to",publication)
}
println

Conclusion

Print, Printf, and Println all have their use case. If you want to output just a message that does not require data type formatting you could use Print. If you require formatting on the string Println and Printf could be of use.

Subscribe to Bluedoa Digest

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
alexander@example.com
Subscribe