Go Print Statements
Go provides four functions to print the output messages on the screen.
fmt.Print()
fmt.Println()
fmt.Printf()
println()
Note: The first three functions are from the fmt
package while the println
is a built-in function from Go and you can directly use this method without importing any package.
Go fmt.Print() function
Let us take an example to demonstrate the fmt.Print()
function
// Program to illustrate Print() function
package main
import "fmt"
// prints output in same lines
func main() {
name := "Chandler Bing"
fmt.Print("Hello", name)
fmt.Print("Welcome to Go Programming")
}
Output
HelloChandler BingWelcome to Go Programming
Notice -
- The output messages are printed in a same line
- It does not additional space while printing the values of a variable.
Go fmt.Println() method
Let us take an example to demonstrate the fmt.Println()
function
// Program to illustrate Println() function
package main
import "fmt"
// prints output in different lines
func main() {
name := "Chandler Bing"
fmt.Println("Hello", name)
fmt.Println("Welcome to Go Programming")
}
Output
Hello Chandler Bing
Welcome to Go Programming
Notice -
- The output messages are printed on a different lines
- It also adds an additional space while printing the values of a variable.
Go println() Method
Suppose that we want to print the value of a variable inside a piece of text. If you only use what we have learned so far, you would have to write something like this:
package main
func main() {
var myNumber int = 42
println("My favorite number is", myNumber, "!")
}
My favorite number is 42 !
Notice -
- it adds extra spaces that we do not necessarily want.
To solve this problem, Go provides a function called fmt.Printf
that allows you to print a formatted string efficiently.
Why there are two println
and Println
functions?
If you are asking yourself why there are two functions with the same behavior (println
and Println
), it is because println
is a built-in function, while Println
is a function from the fmt
package. The former may be removed in the future, so it is better to use the latter.
For the rest of this tutorial, we will consistently use the fmt
package.
Formatted strings
A formatted string is a string that contains placeholders (usually %
followed by a letter) for values that will be replaced by the values of variables.
For instance, the string "My favorite number is %d!"
contains a placeholder for an integer value (%d
). The most common placeholders are:
%d
for integers,%f
for floating-point numbers (you can specify the number of digits after the decimal point, e.g.%.2f
for two digits),%s
for strings,%t
for booleans.
Go fmt.Printf() method
To choose the values that will replace the placeholders, you have to pass them as arguments to the Printf
function from the fmt
package.
package main
import "fmt"
func main() {
var myNumber int = 42
fmt.Printf("My favorite number is %d!\n", myNumber)
}
Output
My favorite number is 42!
Notice -
- The
\n
at the end of the string is a special character that represents a newline. - It was automatically added by the
println
function, but we have to add it manually when usingPrintf
.
More examples
Here are a few more examples of formatted strings:
package main
import "fmt"
func main() {
const pi float64 = 22. / 7.
const message string = "Hello, World!"
const myBool bool = false
fmt.Printf("Pi is approximately %.2f, tau is approximately %.2f\n", pi, 2*pi)
fmt.Printf("The message is \"%s\".\n", message)
fmt.Printf("This sentence is %t.\n", myBool)
}
Output
Pi is approximately 3.14, tau is approximately 6.29
The message is "Hello, World!".
This sentence is false.
Note: We used \"
to print a double quote since "
would have ended the formatted string instead. It is similar to the \n
we used to print a newline. Other special characters include \t
for a tabulation and \r
for a carriage return.
The Print
and Println
functions
In order to print basic strings (just as before with println
), you can use the Print
and Println
functions from the fmt
package.
The difference between them is that Println
automatically adds a newline at the end of the string, while Print
does not.