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.
fmt is a simple implementation of printing things to the terminal.
Printf is an abbreviation of "Print Formatter" unlike
Printf is an advanced implementation of
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.
fmt println is an abbreviation of "Print Line". It is similar to
println fixes the issue of a new line by automatically adding a new line at the end of each output.
Println all have their use case. If you want to output just a message that does not require data type formatting you could use
Printf could be of use.