Skip to content

Go Standard Library

One of Go's greatest strengths is its Standard Library. It is a collection of built-in packages that provide everything you need to build professional software without needing external dependencies.

1. Commonly Used Packages

Package Purpose
fmt Formatted I/O (Printing to console, reading user input).
strings String manipulation (Searching, splitting, joining).
strconv Converting strings to other types (and vice-versa).
math Mathematical constants and functions.
os Operating system functions (File I/O, environment variables).
net/http Building HTTP clients and web servers.
encoding/json Working with JSON data.
time Measuring and displaying time.

2. Importing Packages

You use the import keyword to bring packages into your code. It's best to use "factored" imports for multiple packages.

package main

import (
    "fmt"
    "math"
    "net/http"
)

func main() {
    fmt.Println(math.Pi)
}

3. Import Aliases

You can give a package a custom name if there is a conflict or if you want it to be shorter.

1
2
3
4
5
import f "fmt"

func main() {
    f.Println("Using an alias")
}

4. Side-effect Imports

Sometimes you want to import a package just for its initialization logic (like a database driver), but you don't use any of its functions. We use the blank identifier _ for this.

import _ "github.com/go-sql-driver/mysql"

Tips for Success

  1. Standard Library First: Always check if the standard library can do what you need before looking for a third-party library. It's usually faster, safer, and better documented.
  2. Go Doc: You can read documentation for any package from your terminal: go doc fmt.
  3. Capitalization: Remember, only functions that start with a Capital Letter (e.g., fmt.Println) are "exported" and can be used outside their package.