As illustrated below, both fmt.Println() and println() give same output in Go: Hello world!
But: how do they differ from each other?
Snippet 1, using the fmt package;
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world!")
}
Snippet 2, without the fmt package;
package main
func main() {
println("Hello world!")
}
printlnis an built-in function (into the runtime) which may eventually be removed, while thefmtpackage is in the standard library, which will persist. See the spec on that topic.For language developers it is handy to have a
printlnwithout dependencies, but the way to go is to use thefmtpackage or something similar (logfor example).As you can see in the implementation the
print(ln)functions are not designed to even remotely support a different output mode and are mainly a debug tool.