Go

JSON Encoding

json.Marshal, json.Unmarshal, struct tags, custom marshaling, streaming, RawMessage

20 pytań z rozmów·
Mid-Level
1

Which function allows encoding a Go struct into JSON?

Odpowiedź

json.Marshal encodes a Go struct into JSON bytes. This function reads struct tags to customize serialization (renaming, field omission). For streaming to io.Writer, using json.NewEncoder avoids creating an intermediate buffer in memory.

2

Which function allows decoding JSON into a Go struct?

Odpowiedź

json.Unmarshal decodes JSON bytes into a Go struct. A pointer to the target struct must be passed for data to be correctly written. For streaming from io.Reader, json.NewDecoder is more efficient as it avoids loading all JSON into memory.

3

Which JSON struct tag allows renaming a field during serialization?

Odpowiedź

The json:"custom_name" tag allows renaming a field during JSON serialization. For example, a UserID field can become user_id in JSON. This is useful for respecting naming conventions (snake_case in JSON vs CamelCase in Go).

4

Which JSON struct tag completely excludes a field from serialization?

5

Which JSON struct tag allows omitting a field if its value is empty?

+17 pytań z rozmów

Opanuj Go na następną rozmowę

Uzyskaj dostęp do wszystkich pytań, flashcards, testów technicznych, ćwiczeń code review i symulatorów rozmów.

Zacznij za darmo