tezvyn:

Serialize a Go struct to JSON and contrast with Rust

AI-drafted, machine-checkedSource: go.devbeginner

This tests fluency in Go's encoding/json versus Rust's derive macro ecosystem. Go: call json.Marshal on exported fields; Rust: derive Serialize, then serde_json::to_string. Red flag: claiming Rust uses std-only serialization or that Go needs external crates.

WHAT THIS TESTS: This question checks whether you understand the design philosophy behind each language's serialization stack. Go ships a complete reflection-based JSON encoder in its standard library because Google internal style favored batteries-included tooling with zero dependency management. Rust deliberately keeps serialization out of the standard library and delegates to serde because Rust's trait system and proc macros let third-party crates achieve zero-cost abstraction and type safety that would be hard to evolve inside std.

A GOOD ANSWER COVERS: First, explain that Go uses encoding/json and the json.Marshal function, which relies on reflection to inspect struct fields at runtime. Mention that only exported fields, those starting with an uppercase letter, are serialized, and that struct tags like json name or json name omitempty control the output keys and omission behavior. Second, explain that Rust's idiomatic path is adding serde and serde_json as dependencies, deriving the Serialize trait on a struct, and calling serde_json to_string. Third, contrast the two: Go's approach works immediately with no module system friction but pays a runtime reflection cost and offers less compile-time verification, while Rust's approach requires explicit dependencies and a derive macro but produces strongly typed, often faster code with compile-time guarantees. Fourth, note that Rust's standard library is minimal by design to avoid locking in a single serialization format and to let the ecosystem iterate.

COMMON WRONG ANSWERS: A major red flag is insisting that Rust has a standard-library JSON serializer comparable to Go's encoding/json. Another is claiming that Go requires external packages for basic JSON work. Some candidates also confuse Rust's derive macro with runtime reflection or suggest that Go's struct tags are required for all serialization, missing that field names are used by default. Finally, saying that serde is part of the Rust standard library shows a lack of familiarity with the crate ecosystem.

LIKELY FOLLOW-UPS: An interviewer might ask how you handle unknown or dynamic JSON shapes in each language, which leads to discussing Go's empty interface and type assertions versus Rust's Value type from serde_json. They might probe performance by asking about json.Encoder in Go versus streaming serialization in serde_json, or ask how you skip fields, use custom marshalers in Go, or implement Serialize manually in Rust for legacy formats. A security angle could involve asking how each language handles untrusted input size or recursion limits.

ONE CONCRETE EXAMPLE: In Go, defining a Person struct with Name as a string using the json name tag and Age as an int using the json age tag and then calling json.Marshal on an instance yields a byte slice and an error. The tag renames the field, and omitempty could drop zero values. In Rust, the equivalent is placing derive Serialize above a struct Person with name as String and age as u32 and then calling serde_json to_string with a reference to the instance. The derive generates the trait implementation at compile time, and serde_json handles the actual encoding without runtime reflection on the struct metadata.

Read the original → go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.