The Printf, Print, and Println functions from the fmt package in Go have distinct behaviors:
| Function | Standard Output | Line Break | Formatted Output |
|---|---|---|---|
Print |
Yes | No | No |
Println |
Yes | Yes | No |
Printf |
No | No | Yes |
Differences:
Print: Outputs text without adding a newline at the end.Println: Outputs text and adds a newline at the end, moving the cursor to the next line.Printf: Outputs formatted text using format specifiers (like%s,%d, etc.) but does not add a newline unless explicitly included in the format string.
Example:
package main
import "fmt"
func main() {
fmt.Print("Hello") // Outputs: Hello
fmt.Print("World") // Outputs: HelloWorld
fmt.Println() // Outputs a newline
fmt.Println("LabEx") // Outputs: LabEx
fmt.Printf("%s\n", "Go") // Outputs: Go
}
In this example, you can see how each function behaves differently in terms of output formatting.
