178

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!")
}
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
YulCheney
  • 2,877
  • 2
  • 18
  • 14

5 Answers5

172

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.

nemo
  • 55,207
  • 13
  • 135
  • 135
146

To build upon nemo's answer:

println is a function built into the language. It is in the Bootstrapping section of the spec. From the link:

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

Thus, they are useful to developers, because they lack dependencies (being built into the compiler), but not in production code. It also important to note that print and println report to stderr, not stdout.

The family provided by fmt, however, are built to be in production code. They report predictably to stdout, unless otherwise specified. They are more versatile (fmt.Fprint* can report to any io.Writer, such as os.Stdout, os.Stderr, or even a net.Conn type.) and are not implementation specific.

Most packages that are responsible for output have fmt as a dependency, such as log. If your program is going to be outputting anything in production, fmt is most likely the package that you want.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Alexander Bauer
  • 10,663
  • 8
  • 29
  • 40
  • "but not in production code" what does this mean? When I run `go build`, the output binary still has the text from `println`. – Minh Nghĩa Nov 11 '22 at 04:13
  • It just means that it should not be used in "important" code. Production code is code that is used for some sort of larger project or product that a company or gov't agency uses. In other words, it's fine for a homework assignment, but not for a software engineer who gets paid to do her job. – Mike Williamson Dec 04 '22 at 11:22
7

I can see difference here:

rangeOverIntsAndStrings(1, 5)

func rangeOverIntsAndStrings(args ...interface{}) {
    for _, v := range args {
        println(v)
    }
}

// output

(0x108f060,0x10c5358)
(0x108f060,0x10c5360)

vs

func rangeOverIntsAndStrings(args ...interface{}) {
    for _, v := range args {
        fmt.Println(v)
    }
}

// output

1
5
R Sun
  • 1,353
  • 14
  • 17
-1

Interesting Example:

➜  netpoll git:(develop) ✗ cat test.go
package main

import "fmt"

func main() {
        a := new(struct{})
        b := new(struct{})
        println(a, b, a == b)

        c := new(struct{})
        d := new(struct{})
        fmt.Printf("%v %v %v\n", c, d, c == d)
}
➜  netpoll git:(develop) ✗ go run test.go       
0xc000074f47 0xc000074f47 false
&{} &{} true
➜  netpoll git:(develop) ✗ go run -gcflags="-m" test.go
# command-line-arguments
./test.go:12:12: inlining call to fmt.Printf
./test.go:6:10: new(struct {}) does not escape
./test.go:7:10: new(struct {}) does not escape
./test.go:10:10: new(struct {}) escapes to heap
./test.go:11:10: new(struct {}) escapes to heap
./test.go:12:35: c == d escapes to heap
./test.go:12:12: []interface {} literal does not escape
<autogenerated>:1: .this does not escape
0xc000074f47 0xc000074f47 false
&{} &{} true

It is something difference between println and fmt.Printf.

g10guang
  • 4,647
  • 3
  • 28
  • 22
-2

As for the difference, this is an example.

println() prints a pointer point to the address of function test.

fmt.Println() prints the address of function.

Steven
  • 47
  • 3