Golang : What fmt.Println() can do and println() cannot do
A lot of rookies got confused on why their program is not working as intended when using println()
function. New Golang developers need to know what is difference between fmt.Println()
, fmt.Print()
functions versus println()
and print()
functions. What are they for and why a developer should not use them anymore.
For this tutorial, we will explore what fmt.Println()
can do and println()
can't do
Below is a simple program that demonstrates why Golang developers should not use println()
and print()
functions in their code. As you can see in the output, println()
function prints out a memory address instead of the slice data. Most rookies will be confused by this and couldn't comprehend why their code is not working as intended.
package main
import (
"fmt"
"sort"
)
var game = [11]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var bestgame = [11]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
var bestgames [][]int
func main() {
//for k, v := range game {
// fmt.Println(k, v)
//}
//for i, j := range bestgame {
// fmt.Println(i, j)
//}
if game[10] > bestgame[10] {
bestgame = game
sort.Ints(bestgame[:9])
bestgames = append(bestgames, bestgame[:])
fmt.Println("bestgame", "G", bestgame[0], bestgame[1])
}
for m, q := range bestgames {
fmt.Println(m, q)
}
fmt.Println("the result of println(bestgames = ", bestgames) //<---- use this
println("the result of println(bestgames = ", bestgames) // instead of this. Memory address instead of slice data
}
Output:
bestgame G 0 1
0 [0 1 2 3 4 5 6 7 8 9 10]
the result of println(bestgames = [[0 1 2 3 4 5 6 7 8 9 10]]
the result of println(bestgames = [1/1]0x40a0f0 (this line will be shown as red in play.golang.org)
Ok, now we know it is bad to use println()
and print()
functions in production code. But why these functions are there? What for?
From the official Golang's documentation, these functions are meant for bootstrapping usage and they do not return result.
Current implementations provide several built-in functions useful during bootstrapping. These functions are documented for completeness but are not guaranteed to stay in the language. They do not return a result.
Function Behavior
print prints all arguments; formatting of arguments is implementation-specific
println like print but prints spaces between arguments and a newline at the end
Implementation restriction: print and println need not accept arbitrary argument types, but printing of boolean, numeric, and string types must be supported.
Hope this helps and happy coding!
Reference :
See also : Golang : Sort and reverse sort a slice of integers
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.7k Default cipher that OpenSSL used to encrypt a PEM file
+9.1k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+29.2k Golang : Saving(serializing) and reading file with GOB
+6.8k Golang : How to solve "too many .rsrc sections" error?
+16.3k Golang : Send email and SMTP configuration example
+21.3k Golang : How to read float value from standard input ?
+19k Golang : Calculate entire request body length during run time
+7.6k Golang : How to execute code at certain day, hour and minute?
+5.4k PHP : Fix Call to undefined function curl_init() error
+7.1k Golang : Null and nil value
+11.3k Golang : Delay or limit HTTP requests example
+9.7k Golang : Turn string or text file into slice example