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