Go (Golang) Standard Library: Complete Overview
Go (or Golang) is a modern compiled programming language from Google, designed for simplicity, performance, and reliability. One of the main reasons for its popularity is its powerful and well-documented standard library. Unlike many other languages, Go comes with a rich set of packages "out of the box" that cover virtually all basic tasks: from text processing to building high-load web services. In this overview, we will take a detailed look at the key packages of the Go standard library with code examples.
The fmt Package: Formatted I/O
This is the first package every Go developer encounters. It provides functions for formatted console output and data reading.
package main
import "fmt"
func main() { name := "Go" version := 1.22 // Formatted output fmt.Printf("Language: %s, Version: %.1f", name, version)
// Build string without output message := fmt.Sprintf("Hello, %s!", name) fmt.Println(message)}The strings and strconv Packages: String Manipulation
The strings package contains functions for searching, replacing, splitting, and joining strings. strconv handles converting strings to numbers and vice versa.
import ( "fmt" "strconv" "strings")
func main() { s := "Hello, world!" // Substring search if strings.Contains(s, "world") { fmt.Println("Found") } // String splitting parts := strings.Split(s, ", ") fmt.Println(parts[0]) // Hello
// Convert string to number num, _ := strconv.Atoi("42") fmt.Println(num + 8) // 50}The net/http Package: Building HTTP Servers and Clients
One of the most powerful packages. It allows you to launch a full-fledged HTTP server in just a few lines of code. It is widely used in microservices and web applications.
package main
import ( "fmt" "net/http")
func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])}
func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil)}The encoding/json Package: Working with JSON
Modern applications constantly exchange data in JSON format. The encoding/json package makes it easy to serialize and deserialize Go structures to and from JSON.
import ( "encoding/json" "fmt")
type User struct { Name string `json:"name"` Age int `json:"age"`}
func main() { user := User{Name: "Alex", Age: 30} // Serialize to JSON jsonData, _ := json.Marshal(user) fmt.Println(string(jsonData)) // {"name":"Alex","age":30}
// Deserialize from JSON var decoded User json.Unmarshal(jsonData, &decoded) fmt.Println(decoded.Name) // Alex}The io and os Packages: File and Stream Handling
The os package provides functions for working with the operating system: reading and writing files, working with environment variables and command-line arguments. io contains interfaces for working with data streams (reading/writing).
import ( "io/ioutil" "os")
func main() { // Write to file data := []byte("Hello, Go!") ioutil.WriteFile("example.txt", data, 0644)
// Read from file content, _ := ioutil.ReadFile("example.txt") os.Stdout.Write(content)}The sync Package and Goroutines: Concurrency
Go is famous for its built-in support for concurrency through goroutines (lightweight threads) and channels. The sync package provides synchronization primitives such as mutex
Also in library
Reactive Programming in C# (.NET) — Observable, Rx.NET and LINQ
Working with Kafka in C# (.NET): Confluent.Kafka Library
Working with Markdown in Go (Golang): Libraries, Parsing, and Conversion
PDF Generation in JavaScript: pdf-lib Library