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
+18.9k Golang : Populate dropdown with html/template example
+18.4k Golang : Implement getters and setters
+7.9k Golang : How To Use Panic and Recover
+11.2k Golang : Concatenate (combine) buffer data example
+23.3k Golang : Check if element exist in map
+40.8k Golang : How to count duplicate items in slice/array?
+8.7k Golang : GMail API create and send draft with simple upload attachment example
+5.8k PageSpeed : Clear or flush cache on web server
+13.8k Golang : Fix image: unknown format error
+10.9k Golang : Calculate Relative Strength Index(RSI) example
+30.1k Golang : How to redirect to new page with net/http?
+16.3k Golang : File path independent of Operating System