Demystifying Go (Golang), Google’s Programming Language: Struct and JSON
Introduction
These days, web applications are everywhere, and in this world of constantly communicating APIs and databases, JSON reigns supreme as the go-to data format. It’s like the "English" of system communication: everyone speaks it. But how do we handle data conversion in Go? In this post, we’ll see how Go makes it easy to convert a struct to JSON and back.
Struct to JSON
Go makes data mapping between types simple, without the need for complex functions. All you need to do is define your struct and, within backticks (`), map each field to JSON. Here’s a basic example of a Product struct.
Inside the backticks, we specify the mapping type (json), and after the colon ":", we set the field name in JSON. For example: json:"id".
Note: Notice that the field names in the struct start with uppercase letters. In Go, uppercase means "public" (visible outside the package), while lowercase means "private" (only visible within the package). So, to convert to JSON, your fields must be "public." Don’t worry too much about this now; we’ll go over visibility and scopes in more detail in the package chapter.
To perform the conversion to JSON, we use the Marshal function from Go’s native encoding/json library. Here’s how it works:
Note that the Marshal function returns two values: the JSON in bytes and a possible error. It’s essential to handle this error in your application, so if something goes wrong, you can address it before an incorrect value causes a bug later on. For printing, we convert the byte array to a string.
Recommended by LinkedIn
JSON to Struct
JSON-to-struct conversion is quite common in APIs, where we receive data in the request body and need to map it to our Go structs. To do this, we use the inverse function, Unmarshal, which takes the JSON byte array and an any interface representing the target type. This function only returns an error (if one occurs) and fills the struct with the data. Let’s see an example:
IMPORTANT: Although the second parameter is any, it must be a pointer to the desired struct; otherwise, Go won’t be able to insert the data into the correct fields.
If something goes wrong — like the string being malformed — Unmarshal will return an error, and the struct won’t be populated. Here’s an example of an error:
Conclusion
Converting structs to JSON and JSON to structs in Go is straightforward and efficient, which greatly simplifies API development and integration with other applications. Go gives us easy-to-use tools with error control to ensure every step is managed, which is essential for avoiding headaches.
With this, you’re ready to work with JSON in Go. For your code, this means fewer surprises and more efficiency — the hallmark of good code in Go!
Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS
5moAmazing
FullStack Developer @ Itaú Digital Assets | Go | TS | Blockchain | Aws
5moGreat content!
Data Scientist | Python | Pyspark | SQL | Machine Learning | Databricks
6moNice tips! Thanks for sharing!
Data Analyst | GCP | SQL
6moInteresting, thanks for sharing! 👏
Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)
6moInteresting