Go Output Functions:
Go provides several built-in functions in the fmt package to display output. These are primarily used for printing to the console, and formatting strings.
1. fmt.Print()
This prints the text without any newline character at the end.
go
package main
import "fmt"
func main() {
fmt.Print("Hello ")
fmt.Print("World!")
}2. fmt.Println()
This function prints the output followed by a new line.
go
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}3. fmt.Printf()
Use this for formatted output using verbs like %s for strings, %d for integers, etc.
go
package main
import "fmt"
func main() {
name := "Alice"
age := 30
fmt.Printf("Name: %s, Age: %d\n", name, age)
}4. Other fmt Verbs
%v– default format%T– type of value%.2f– floating point with precision%t– boolean
go
package main
import "fmt"
func main() {
price := 45.6789
isAvailable := true
fmt.Printf("Price: %.2f, Available: %t\n", price, isAvailable)
}Summary
- Print: outputs without newline
- Println: outputs with newline
- Printf: outputs with format control