Go (Golang) Standard Library: Complete Overview
The Go language (Golang), developed at Google, is renowned for its conciseness, high performance, and built-in concurrency support. However, one of its main "superpowers" is the powerful and well-designed standard library. Unlike many other languages where you need to add third-party dependencies for basic tasks, Go provides everything you need "out of the box." In this article, we will provide a complete overview of the key packages in the Go standard library that every developer will find useful.
1. Input/Output and File Operations: io, os, bufio packages
The io package is the foundation for all I/O operations. Its io.Reader and io.Writer interfaces are among the most important abstractions in Go. In fact, any data source (file, network connection, string) can be represented as a Reader, and any data sink as a Writer.
The os package provides functions for interacting with the operating system: opening/creating files, reading directories, working with environment variables.
The bufio package adds buffering, which significantly speeds up reading and writing large amounts of data.
package main
import ( "bufio" "fmt" "os")
func main() { // Open file for reading file, err := os.Open("example.txt") if err != nil { panic(err) } defer file.Close()
// Create a buffered scanner scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) }
if err := scanner.Err(); err != nil { fmt.Fprintln(os.Stderr, "Read error:", err) }}2. Text Processing and Formatting: strings, fmt, strconv packages
The strings package contains hundreds of useful functions for working with strings: searching, replacing, splitting, joining, trimming whitespace, and much more. The strconv package handles converting strings to numbers (and back), as well as parsing boolean values.
The fmt package is a "Swiss Army knife" for formatted output. It implements the Printf, Sprintf, and Fprintf functions, which work with any type that implements the Stringer interface.
package main
import ( "fmt" "strconv" "strings")
func main() { // Splitting a string parts := strings.Split("a,b,c", ",") fmt.Println(parts) // [a b c]
// Converting string to number num, _ := strconv.Atoi("42") fmt.Printf("Number: %d", num) // Number: 42
// Formatting with field width fmt.Printf("|%10s|%5d|", "Hello", 123)}3. Networking and HTTP: net, net/http packages
The net package provides low-level primitives for working with TCP/UDP, DNS, and IP addresses. But the real "star" is the net/http package. It allows you to create full-featured HTTP servers and clients without a single third-party dependency.
Thanks to the built-in http.ServeMux and middleware approach, you can easily route requests, handle JSON, manage cookies and sessions. The Go standard library is used in production by giants such as Docker, Kubernetes, and Dropbox.
package main
import ( "encoding/json" "fmt" "net/http")
type Response struct { Message string `json:"message"`}
func helloHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not supported", http.StatusMethodNotAllowed) return }
resp := Response{Message: "Hello, world!"} w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(resp)}
func main() { http.HandleFunc("/api/hello", hello