Loading...

Structs and Methods in Go: A Journey Through the Land of Code

Go, or Golang, as it’s affectionately known, is a statically-typed programming language that’s quickly becoming a favorite among developers. Its simplicity, efficiency, and scalability make it an excellent choice for building robust applications. And if you’re just starting out with Go or want to enhance your skills, this guide is for you! Today, we’ll be taking a thrilling adventure through the world of Structs and Methods in Go.

Structs: The Building Blocks of Your App

Structs in Go are composite data types that allow you to group values of different data types into a single unit. They’re similar to objects in object-oriented programming (OOP) languages and are essentially user-defined data types.

Imagine you want to store information about a person, such as their name, age, and address. With structs, you can do that with ease! Here’s an example:

type Person struct { name string age int address string }
Code language: Go (go)

In the code above, we’ve defined a struct named Person that has three fields – name, age, and address. The type of each field is specified after the field name. To create an instance of the struct, you can use the following syntax:

p := Person{name: "Mehul Mohan", age: 28, address: "123 Main Street"}
Code language: Go (go)

In the code above, we’ve created an instance of the Person struct and assigned values to its fields. It’s that simple!

Methods: Unlocking the Power of Structs

Methods in Go are like functions, but they’re bound to a specific type. They’re similar to member functions in OOP languages and allow you to perform operations on structs.

For example, let’s say you want to add a method to the Person struct we defined earlier. This method will return the person’s full name. Here’s how you can do that:

func (p Person) fullName() string { return p.name }
Code language: Go (go)

In the code above, we’ve defined a method named fullName that is associated with the Person type. The syntax for defining a method is as follows:

func (receiverType receiverName) methodName(args ...) returnType { // Method body }
Code language: Go (go)

The receiverType and receiverName specify the type and the name of the struct that the method is associated with. The methodName specifies the name of the method, and the args and returnType specify the arguments and the return type of the method, respectively.

To call the method, you can use the following syntax:

fmt.Println(p.fullName()) // Output: Mehul Mohan
Code language: Go (go)

In the code above, we’ve called the fullName method on the Person struct p and printed the result to the console.

Struct Embedding: Inheritance, Go-style

Struct embedding is a feature in Go that allows one struct to inherit the fields and methods of another struct. In other words, one struct can be embedded within another struct.

For instance, let’s say you want to add another struct to the Person struct that will store information about the person’s occupation. Here’s how you can do that:

type Occupation struct { company string role string } type Person struct { name string age int address string Occupation }
Code language: Go (go)

In the code above, we’ve defined a struct Occupation with two fields – company and role. Then, we’ve embedded the Occupation struct within the Person struct. This means that the Person struct now has access to the fields and methods of the Occupation struct.

To access the fields of the embedded struct, you can use the following syntax:

p := Person{ name: "Mehul Mohan", age: 28, address: "123 Main Street", Occupation: Occupation{ company: "codedamn", role: "Author" } } fmt.Println(p.company) // Output: codedamn fmt.Println(p.role) // Output: Author
Code language: Go (go)

In the code above, we’ve created an instance of the Person struct and assigned values to its fields, including the fields of the embedded Occupation struct. Then, we’ve accessed the fields of the embedded struct and printed the values to the console.

Anonymous Structs: A Quick Fix

Anonymous structs in Go are structs that don’t have a named type. They’re defined and used inline within a function or an expression and are great for storing temporary data without having to define a new struct type.

For instance, let’s say you want to store information about a person’s contact information. Here’s how you can do that using anonymous structs:

func main() { p := struct { name string phone string email string }{ name: "Mehul Mohan", phone: "555-555-5555", email: "[email protected]", } fmt.Println(p.name) // Output: Mehul Mohan fmt.Println(p.phone) // Output: 555-555-5555 fmt.Println(p.email) // Output: [email protected] }
Code language: Go (go)

In the code above, we’ve defined an anonymous struct inline within the main function and assigned values to its fields. Then, we’ve accessed the fields of the anonymous struct and printed the values to the console.

In Conclusion

In this guide, we’ve gone on a journey through the world of Structs and Methods in Go and seen how they can be used to build amazing applications. We’ve discussed the basics of structs, methods, struct embedding, and anonymous structs. Whether you’re a beginner or an experienced programmer, I hope this guide has given you a deeper understanding of these concepts and how they can be used in practice. Structs and Methods are fundamental concepts in Go and are widely used in real-world applications. So, don’t hesitate to continue exploring the world of Go and its many features! Have fun coding! 💻

Sharing is caring

Did you like what Mehul Mohan wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: