0PricingLogin
Go Academy · Lesson

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

  1. Reading Files with os and bufio
  2. Writing Files and Temp Files
  3. JSON Encoding and Decoding
  4. Streaming JSON and CSV
← Back to Go Academy