Coding/Learn/go/go json and http
Go Foundational Course
01.Introduction, Toolchain & Package main
02.Variables, Zero Values, Constants & iota
03.Control Flow: If with Init Statements & Switch
04.The Unified For Loop & Range Iteration
05.Functions, Multiple Return Values & Named Returns
06.Arrays vs Slices: Dynamic Memory & Headers
07.Maps: Key-Value Hash Tables & Safe Access
08.Pointers & Value vs Pointer Semantics
09.Structs, Receiver Methods & Composition
10.Implicit Interfaces & Duck Typing with Typesafety
11.Explicit Error Handling, Wrapping & Inspection
12.Defer, Panic & Recover: Resource Cleanup
13.Strings, Runes & UTF-8 Encoding Internals
14.Goroutines & The M:N Work-Stealing Scheduler
15.Channels: CSP Concurrency & Pipelines
16.Select Statement, Non-Blocking IO & Timeouts
17.Mutexes, RWMutex & Atomic Primitives
18.Context Propagation, Cancellation & Deadlines
19.JSON Serialization, Tags & net/http Routing
20.Capstone: High-Performance Concurrent Microservice Engine
Course Certificate0%

Complete all lessons to earn

LearnJSON Serialization, Tags & net/http Routing
19 / 20 0%
Lesson 19 of 20
16 mins read

JSON Serialization, Tags & net/http Routing

PreviousNext Lesson
In Plain English

The Go standard library features industry-grade networking and JSON parsing out of the box. Learn json.Marshal/Unmarshal, struct field tags (`json:"key,omitempty"`), and net/http request multiplexing.

The Mental Model

"Struct tags are like customs declaration labels on cargo packages ("tag: name, omitempty"). The Go JSON engine inspects them to format data for foreign systems."

Deep Dive: How It Works

JSON Struct Tags: `json:"field_name,omitempty"` customizes serialization naming and omits zero values.

Exported Visibility: Only struct fields beginning with capital letters can be accessed and serialized by encoding/json.

net/http Architecture: http.NewServeMux handles high-throughput HTTP routing natively with zero third-party dependencies.

Syntax Blueprint

type User struct {
    Name string `json:"name"`
    Age  int    `json:"age,omitempty"`
}
bytes, _ := json.Marshal(u)

Define struct with json tags, marshal structs to JSON bytes, and unmarshal JSON bytes back to structs.

PointCore Rules to Remember

Point star
Exported Fields Only: Lowercase fields are unexported (private) and will be ignored by json.Marshal.
Point star
Zero Dependencies: Go net/http is production-grade and powers major cloud services directly.
Point star
Struct Tags: Customize serialization keys and strip sensitive fields with `json:"-"`.

Common Beginner Traps & How to Fix Them

Mistake:Using unexported lowercase fields: type User struct { name string } and getting empty JSON {}.

Why it happens: encoding/json uses reflection and cannot access unexported fields.

How to fix: Capitalize field names: Name string `json:"name"`.

Pro Tip: In Go 1.22+, http.NewServeMux supports HTTP method and path wildcard matching directly: mux.HandleFunc("GET /users/{id}", handler).

Live Interactive Example

Hit Run Code to see it live
JSON Encoding and Decoding with Struct Tags
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Output Console
Click "Run Code" to view the rendered output.
How it works: Struct tags mapped ServiceName to service_name in JSON, and unmarshaling safely populated the struct.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Parse User Profile JSON

  • PointDefine a UserProfile struct with Name (string) and Karma (int) with appropriate JSON tags.
  • PointUnmarshal the JSON `{"name":"Alice","karma":1500}` into a UserProfile struct.
  • PointPrint "User: [Name], Karma: [Karma]".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates