Standard Library
Go's rich stdlib vs C's minimal stdlib — fmt, strings, os, net/http
Introduction
In this lesson, you'll learn about standard library in Go. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with go's rich stdlib vs c's minimal stdlib — fmt, strings, os, net/http.
Go has its own approach to go's rich stdlib vs c's minimal stdlib — fmt, strings, os, net/http, which we'll explore step by step.
The Go Way
Let's see how Go handles this concept. Here's a typical example:
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
"os"
"strings"
"strconv"
)
func main() {
// fmt — formatted I/O
fmt.Printf("Hello, %s! Age: %d\n", name, age)
s := fmt.Sprintf("Value: %d", n)
// strings — string operations (immutable)
strings.ToUpper("hello") // "HELLO"
strings.Split("a,b,c", ",") // ["a","b","c"]
strings.Contains("hello", "ell") // true
strings.TrimSpace(" hi ") // "hi"
// strconv — type conversions
n, _ := strconv.Atoi("42") // string → int
strconv.Itoa(n) // int → string
// os — file system and environment
data, _ := os.ReadFile("file.txt")
os.WriteFile("out.txt", data, 0644)
os.Getenv("HOME")
// math
math.Sqrt(2.0); math.Pow(2, 10); math.Abs(-3.14)
// JSON — no external library needed
type User struct { Name string `json:"name"` }
u := User{"Alice"}
b, _ := json.Marshal(u)
json.Unmarshal(b, &u)
// HTTP server — production-ready in stdlib
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello!")
})
http.ListenAndServe(":8080", nil)
}Comparing to C
Here's how you might have written similar code in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// printf / scanf
printf("Hello, %s! Age: %d\n", name, age);
scanf("%49s", name);
// String ops
strlen(s); strcpy(dst, src); strcmp(s1, s2);
strstr(s, "sub"); strchr(s, 'c');
snprintf(buf, sizeof(buf), "Value: %d", n);
// Memory
void *p = malloc(100); free(p);
memset(buf, 0, sizeof(buf));
memcpy(dst, src, n);
// Math
sqrt(x); pow(base, exp); fabs(x);
sin(x); cos(x); ceil(x); floor(x);
// No HTTP, no JSON, no concurrency in stdlib
// Need: libcurl (HTTP), cJSON (JSON), pthreads (threads)You may be used to different syntax or behavior.
Go stdlib includes HTTP server, JSON, crypto, compression — C needs external libraries
You may be used to different syntax or behavior.
strings package has all string operations; Go strings are immutable UTF-8
You may be used to different syntax or behavior.
os.ReadFile/WriteFile replace fopen/fread/fclose — automatic resource management
You may be used to different syntax or behavior.
fmt.Sprintf is the safe equivalent of snprintf — no buffer overflow possible
You may be used to different syntax or behavior.
net/http is production-grade — Docker's HTTP layer uses it
Step-by-Step Breakdown
1. fmt Package
fmt.Printf/Sprintf replace C's printf/snprintf. No buffer overflow — Go handles string allocation automatically.
char buf[100];
snprintf(buf, sizeof(buf), "x=%d", x);s := fmt.Sprintf("x=%d", x) // no buffer needed
fmt.Printf("x=%d\n", x) // like printf
fmt.Println("hello") // adds newline2. strings Package
All string operations in one package. Strings are immutable — all functions return new strings.
strlen(s); strcpy(d, s); strcmp(a,b); strstr(s, "x")strings.ToUpper(s)
strings.Split(s, ",")
strings.Contains(s, "x")
strings.ReplaceAll(s, "old", "new")3. os Package
os.ReadFile/WriteFile are safe, simple wrappers. No manual fopen/fclose — resource management is handled.
FILE *f = fopen(path, "r");
// ... read ... fclose(f);data, err := os.ReadFile(path)
if err != nil { return err }
os.WriteFile("out.txt", data, 0644)4. net/http
Go's net/http is a full HTTP server in the stdlib — no Apache or nginx needed for simple services.
// C: needs libmicrohttpd or libcurl — no stdlib HTTPmux := http.NewServeMux()
mux.HandleFunc("GET /users", getUsers)
mux.HandleFunc("POST /users", createUser)
log.Fatal(http.ListenAndServe(":8080", mux))Common Mistakes
When coming from C, developers often make these mistakes:
- Go stdlib includes HTTP server, JSON, crypto, compression — C needs external libraries
- strings package has all string operations; Go strings are immutable UTF-8
- os.ReadFile/WriteFile replace fopen/fread/fclose — automatic resource management
Key Takeaways
- fmt.Sprintf replaces snprintf — no buffer, no overflow
- strings: ToUpper, Split, Contains, TrimSpace, ReplaceAll — immutable returns
- os.ReadFile/WriteFile replace fopen/read/fclose — automatic resource management
- net/http is production-ready stdlib — JSON, HTTP server, crypto all included