Search code examples
goprintln

Difference between fmt.Println() and println() in Go


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!")
}

Solution

  • println is an built-in function (into the runtime) which may eventually be removed, while the fmt package is in the standard library, which will persist. See the spec on that topic.

    For language developers it is handy to have a println without dependencies, but the way to go is to use the fmt package or something similar (log for 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.