Go

JSON Encoding

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

20 câu hỏi phỏng vấn·
Mid-Level
1

Which function allows encoding a Go struct into JSON?

Câu trả lời

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?

Câu trả lời

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?

Câu trả lời

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 câu hỏi phỏng vấn

Nắm vững Go cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí