JSON Encoding and Decoding
Marshal, Unmarshal, and struct tags
encoding/json overview
Go's encoding/json package marshals Go values to JSON and unmarshals JSON back to Go values using struct tags and reflection.
json.Marshal
json.Marshal converts a Go value to JSON bytes. Returns an error for unsupported types (channels, functions, complex numbers).
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
u := User{Name: "Alice", Age: 30}
data, err := json.Marshal(u)
fmt.Println(string(data)) // {"name":"Alice","age":30}All lessons in this course
- Reading Files with os and bufio
- Writing Files and Temp Files
- JSON Encoding and Decoding
- Streaming JSON and CSV