How to Print Output in Go

fmt.Println, Printf, and Sprintf

Use fmt.Println for simple output, fmt.Printf for formatted printing, and fmt.Sprintf for formatted string creation in Go.

Use fmt.Println for simple output with newlines, fmt.Printf for formatted strings, and fmt.Sprintf to format strings into variables without printing.

package main

import "fmt"

func main() {
  fmt.Println("Hello, World!")
  fmt.Printf("Value: %d\n", 42)
  s := fmt.Sprintf("Formatted: %s", "text")
  fmt.Println(s)
}