Demystifying Go (Golang), Google’s Programming Language: Struct and JSON

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.

Article content

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:

Article content

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.

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:

Article content

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:

Article content

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!

Patrick Cunha

Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS

5mo

Amazing

Like
Reply
Jefferson Luiz

FullStack Developer @ Itaú Digital Assets | Go | TS | Blockchain | Aws

5mo

Great content!

Like
Reply
Giovani Rodrigues

Data Scientist | Python | Pyspark | SQL | Machine Learning | Databricks

6mo

Nice tips! Thanks for sharing!

Like
Reply

Interesting, thanks for sharing! 👏

Like
Reply
Leandro Veiga

Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)

6mo

Interesting

Like
Reply

To view or add a comment, sign in

More articles by Vagner Nascimento

Insights from the community

Others also viewed

Explore topics