C
GO

C to Go

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Slices4Structs & Methods5Pointers6Concurrency7Header Files → Packages8Error Handling9Testing10Standard Library
All Mirror Courses
C
GO
Standard Library
MirrorLesson 10 of 10
Lesson 10

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.

Mirror Card
C
From C:

In C, you're familiar with go's rich stdlib vs c's minimal stdlib — fmt, strings, os, net/http.

GO
In Go:

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:

GO
Go 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:

C
C (What you know)
#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)
Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go stdlib includes HTTP server, JSON, crypto, compression — C needs external libraries

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

strings package has all string operations; Go strings are immutable UTF-8

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

os.ReadFile/WriteFile replace fopen/fread/fclose — automatic resource management

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

fmt.Sprintf is the safe equivalent of snprintf — no buffer overflow possible

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

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.

C
C
char buf[100];
snprintf(buf, sizeof(buf), "x=%d", x);
GO
Go
s := fmt.Sprintf("x=%d", x)  // no buffer needed
fmt.Printf("x=%d\n", x)       // like printf
fmt.Println("hello")           // adds newline

2. strings Package

All string operations in one package. Strings are immutable — all functions return new strings.

C
C
strlen(s); strcpy(d, s); strcmp(a,b); strstr(s, "x")
GO
Go
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.

C
C
FILE *f = fopen(path, "r");
// ... read ... fclose(f);
GO
Go
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
C
// C: needs libmicrohttpd or libcurl — no stdlib HTTP
GO
Go
mux := 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
Common Pitfall
Don't assume Go works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Go to practice these concepts.
PreviousFinish