fmt and strings Packages
Formatting output and manipulating strings
The fmt Package Overview
The fmt package implements formatted I/O. Key functions:
fmt.Print,fmt.Println,fmt.Printf— write to stdoutfmt.Sprint,fmt.Sprintf,fmt.Sprintln— return stringsfmt.Fprint,fmt.Fprintf— write to any io.Writerfmt.Scan,fmt.Scanf— read from stdin
fmt Format Verbs
Common format verbs for fmt.Printf:
package main
import "fmt"
func main() {
name := "Gopher"
age := 15
score := 98.6
fmt.Printf("%s is %d years old\n", name, age)
fmt.Printf("Score: %.1f%%\n", score)
fmt.Printf("Type: %T, Value: %v\n", age, age)
fmt.Printf("Hex: %x, Binary: %b\n", 255, 255)
fmt.Printf("Padded: %10s|%-10s\n", name, name)
}All lessons in this course
- fmt and strings Packages
- strconv, math and sort
- time Package Essentials
- os and filepath Packages