Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

println

People also ask

What is FMT Println?

The fmt. Println() function in Go language formats using the default formats for its operands and writes to standard output. Here spaces are always added between operands and a newline is appended at the end. Moreover, this function is defined under the fmt package.

What is the difference between Println and printf in go?

Here Printf formats according to a specified format specifier but Println uses the default formats for its operands. How to Install Go on Windows?

What does FMT mean in Golang?

fmt stands for the Format package. This package allows to format basic strings, values, or anything and print them or collect user input from the console, or write into a file using a writer or even print customized fancy error messages. This package is all about formatting input and output.

What is Println go?

We use Println() to write the input data stream to the standard output. It is a variadic function, which means that it takes a variable number of input arguments. The functionality of Println() in Golang is similar to print in python or printf in C.


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.


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.


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