Demystifying Go (Golang), Google’s Programming Language: Structs - Part 1

Demystifying Go (Golang), Google’s Programming Language: Structs - Part 1

Introduction

Now that you’re practically a “Gopher” expert in functions, let’s take the next step and dive into the mighty structs! Structs aren’t new for those who’ve dabbled in C, but in Go, they have a unique flavor. Let's uncover the power of these structures and see how they can help organize your code as if they were objects… well, almost!


Getting to Know Structs

Structs are Go’s go-to way to represent complex data. Imagine you want to store product information, like its name, price, and discount. Sounds a bit like an object in object-oriented programming (OOP), right? Almost! In Go, structs are different: they have no inheritance or concept of instantiation like in OOP. Instead, we create a struct type and list the fields with their names and types. Here’s how it works:

In the example above, we created a Product type with three fields to represent a product and its attributes. Next, in the main function, we create a variable of type Product and print it. Since we haven’t assigned any values, each struct field takes its respective zero value. Now, let’s see how to assign values to struct fields:

You can also access a specific field of the struct using myVar.Field, as shown in the example below:


Receivers: Go’s “Methods”

Now that you know how to declare structs, let’s talk about adding functions that operate on them. In Go, we have the concept of a receiver, which allows us to associate a function with a specific struct. This lets us call the function on a struct variable as if it were a method in OOP. Check out the example where we create a function GetPriceWithDiscount and associate it with Product as its receiver:

Easy, right? Now let’s say we want to create a function to modify a struct field—say SetName, to change the product’s name. Let’s create this function and see what happens.

Huh? Where’s the updated name? Why didn’t it print? Don’t worry; here’s the explanation. When you associate a function with a struct as its receiver, Go uses the struct’s value, not its pointer. So, any changes are made on the copy, not on the original struct. To make real changes to fields, you just need to use a pointer receiver (p *Product). See the example below:


Conclusion

With structs and receivers, Go provides a straightforward way to organize data and add functionality without the complexity of inheritance or classes. While structs may seem like OOP objects, their simplicity is what makes them so powerful in Go. In the next part, we’ll look at how to take structs even further with advanced topics like composition and interfaces, making your code even more flexible and readable.

Rafael Andrade

Senior Data Engineer | Azure | AWS | Databricks | Snowflake | Apache Spark | Apache Kafka | Airflow | dbt | Python | PySpark | Certified

4 个月

Great insights! Thanks for sharing, Vagner Nascimento.

André Ramos

Senior Software Engineer | Java | Spring Boot | Micro Services | Fullstack Software Developer | Angular | AWS | TechLead

4 个月

Good one! Your posts are excellent. The themes and the content always with a good approach! Congratulations!

Erick Zanetti

Fullstack Engineer | Software Developer | React | Next.js | TypeScript | Node.js | JavaScript | AWS

4 个月

Great advice

Vinicius Bergamin

Senior SQL Developer | Database Administrator | AWS | Performance Tuning | Oracle | Postgres | MongoDB | Data Engineer

4 个月

Interesting

要查看或添加评论,请登录

Vagner Nascimento的更多文章

社区洞察

其他会员也浏览了