
JSON Encoding
json.Marshal, json.Unmarshal, struct tags, custom marshaling, streaming, RawMessage
1Which function allows encoding a Go struct into JSON?
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.
2Which function allows decoding JSON into a Go struct?
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.
3Which JSON struct tag allows renaming a field during serialization?
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).
Which JSON struct tag completely excludes a field from serialization?
Which JSON struct tag allows omitting a field if its value is empty?
+17 면접 질문