Go Input and Output
Input and output form the backbone of interactive programs. Output sends information to the screen. Input reads data that a user types into the terminal. Go handles both through the fmt package.
Output Functions in fmt
| Function | What It Does |
|---|---|
fmt.Println() | Prints text and adds a new line at the end |
fmt.Print() | Prints text without a new line |
fmt.Printf() | Prints formatted text using format verbs |
fmt.Sprintf() | Builds a formatted string and returns it (does not print) |
Using fmt.Println
package main
import "fmt"
func main() {
fmt.Println("Hello, World!") // prints and moves to next line
fmt.Println("Name:", "Alice") // can print multiple values
fmt.Println(10 + 5) // prints: 15
}
Output:
Hello, World!
Name: Alice
15
Using fmt.Printf with Format Verbs
Format verbs are placeholders that tell Go how to display a value. They start with %.
| Verb | Meaning | Example Output |
|---|---|---|
%d | Integer | 42 |
%f | Float | 3.140000 |
%.2f | Float with 2 decimal places | 3.14 |
%s | String | Alice |
%t | Boolean | true |
%v | Default format (any type) | Depends on value |
%T | Type of the value | int, string, etc. |
\n | New line | Moves cursor down |
package main
import "fmt"
func main() {
name := "Alice"
age := 25
score := 98.75
fmt.Printf("Name: %s\n", name)
fmt.Printf("Age: %d\n", age)
fmt.Printf("Score: %.2f\n", score)
fmt.Printf("Type of age: %T\n", age)
}
Output:
Name: Alice
Age: 25
Score: 98.75
Type of age: int
Using fmt.Sprintf to Build Strings
fmt.Sprintf works exactly like fmt.Printf but returns the formatted string instead of printing it. This is useful for building messages to store or pass to other functions.
package main
import "fmt"
func main() {
name := "Bob"
message := fmt.Sprintf("Welcome, %s! You have %d new messages.", name, 3)
fmt.Println(message)
}
Output:
Welcome, Bob! You have 3 new messages.
Reading Input with fmt.Scan
fmt.Scan reads values typed by the user and stores them in variables. Pass the variable address using &.
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter your name: ")
fmt.Scan(&name)
fmt.Println("Hello,", name)
}
Terminal interaction:
Enter your name: Alice
Hello, Alice
Reading Multiple Inputs
package main
import "fmt"
func main() {
var name string
var age int
fmt.Print("Enter name and age: ")
fmt.Scan(&name, &age)
fmt.Printf("Name: %s, Age: %d\n", name, age)
}
Terminal interaction:
Enter name and age: Alice 25
Name: Alice, Age: 25
Reading a Full Line with fmt.Scanln
fmt.Scan stops reading at a space. fmt.Scanln reads until the user presses Enter.
package main
import "fmt"
func main() {
var city string
fmt.Print("Enter city name: ")
fmt.Scanln(&city)
fmt.Println("City:", city)
}
Input/Output Flow Diagram
User types input
│
▼
fmt.Scan / fmt.Scanln ──► stores value in variable
│
▼
Program processes the value
│
▼
fmt.Println / fmt.Printf ──► displays output to screen
Key Points
fmt.Printlnadds a new line;fmt.Printdoes notfmt.Printfuses format verbs like%s,%d,%ffor precise formattingfmt.Sprintfbuilds and returns a formatted string without printingfmt.Scanreads user input — always pass the variable address with&fmt.Scanlnreads a full line until the user presses Enter
