How To Use Struct In Go Lang?
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
In Go, a struct is a composite data type that allows you to group together values of different data types into a single entity. Structs are a powerful tool that can be used in a variety of ways in Go, including creating custom data types, representing objects, and passing data between functions. In this article, we'll explore the different scenarios and uses of structs in Go with examples.

To create a struct in Go, you use the type keyword, followed by the name of the struct and its fields. Here's the syntax:
type StructName struct {
Field1 FieldType1
Field2 FieldType2
...
}
In the above syntax, StructName is the name of the struct, and Field1, Field2, etc. are the names of the fields. FieldType1, FieldType2, etc. are the types of the fields.
To create a struct in Go, you use the type keyword, followed by the name of the struct and its fields. Here's an example:
type Person struct {
name string
age int
}
In the above example, we define a Person struct with two fields, name and age. The name field is of type string, and the age field is of type int.
You can initialize a struct in Go using a struct literal. Here's an example:
person := Person{name: "John", age: 30}
In the above example, we initialize a Person struct with the name "John" and age 30.
You can access the fields of a struct in Go using the dot notation. Here's an example:
fmt.Println(person.name) // "John"
fmt.Println(person.age) // 30
In the above example, we access the name and age fields of the person struct.
Passing Structs to Functions: You can pass structs to functions in Go just like any other variable. Here's an example:
func printPerson(person Person) {
fmt.Printf("Name: %s, Age: %d\n", person.name, person.age)
}
func main() {
person := Person{name: "John", age: 30}
printPerson(person)
}
In the above example, we define a printPerson() function that takes a Person struct as a parameter and prints its fields.
In Go, you can embed one struct inside another to create a new struct with additional fields. Here's an example:
type Address struct {
street string
city string
country string
}
type Person struct {
name string
age int
address Address
}
func main() {
person := Person{
name: "John",
age: 30,
address: Address{
street: "123 Main St",
city: "New York",
country: "USA",
},
}
fmt.Printf("Name: %s, Age: %d, Street: %s, City: %s, Country: %s\n",
person.name, person.age, person.address.street, person.address.city, person.address.country)
}
In the above example, we define an Address struct with three fields, street, city, and country. We then define a Person struct that embeds the Address struct as a field. We initialize a Person struct with a name, age, and address, and then access its fields using the dot notation.
Structs are a powerful tool in Go that can be used in a variety of scenarios and use cases. Whether you're creating custom data types, representing objects, or passing data between functions, structs offer a flexible and efficient way to organize and manipulate data in your Go programs. By understanding how to create, initialize, access, and pass structs to functions, you can take full advantage of the power and flexibility of this important Go feature.