Go

JSON Encoding

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

20 питань зі співбесід·
Mid-Level
1

Which function allows encoding a Go struct into JSON?

Відповідь

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?

Відповідь

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?

Відповідь

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 питань зі співбесід

Опануй Go для наступної співбесіди

Отримай доступ до всіх питань, flashcards, технічних тестів, вправ code review та симуляторів співбесід.

Почни безкоштовно