From log.go (the implementation of the log package) :
167 // Println calls l.Output to print to the logger. 168 // Arguments are handled in the manner of fmt.Println. 169 func (l *Logger) Println(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
log.Println
is just a function wrapper for fmt.Sprintln
, why should I use it instead of fmt.Println
or fmt.Sprintln
?
Any practical reasons ?
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.
Here Printf formats according to a specified format specifier but Println uses the default formats for its operands.
The fmt. out. printf() function in Golang allows users to print formatted data. The function takes a template string that contains the text that needs to be formatted, plus some annotation verbs that tell the fmt functions how to format the trailing arguments.
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.
Two things are different:
Printing via package log is safe from concurrent goroutines (while plain fmt
isn't)
Log can add timing information automatically.
So these are two completely different things. log is for logging and fmt
for formatting. (Okay, log uses the same verbs and flags, but that is just convenient).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With