Go provides the built-in
print and println functions as well as the fmt package's formatted output functions. They are not interchangeable for application logging or structured values.Use fmt.Println for ordinary output
go_snippet
Go · 8 lines|
| package main |
| import "fmt" |
| func main() { |
| values := []int{1, 2, 3} |
| fmt.Println("Values:", values) |
| } |
fmt.Println writes to standard output, inserts spaces between arguments, and ends with a newline. It formats common values such as slices and structs; types may also customize formatting through interfaces such as fmt.Stringer.Built-in print and println
go_snippet
Go · 5 lines|
| package main |
| func main() { |
| println("debug value:", 42) |
| } |
The built-ins are useful for simple bootstrapping and debugging, but the language specification does not guarantee their output format or destination. They do not provide the general formatting behavior of
fmt, and not every value can be printed directly. Output interleaved with fmt.Println may appear in an unexpected order because destinations and buffering can differ.Which should you choose?
Use
fmt.Println, fmt.Printf, or a logging package for normal program output. Reserve print and println for temporary low-level debugging.✦ 卷终 · 柯航存卷 ✦