Structures in Golang
In Go, a structure (struct) is a composite data type that groups together zero or more variables with different data types under a single name. Structs provide a way to create user-defined data types that represent more complex data structures.
Here are some key points about structures in Go:
- A struct is defined using the
struct
keyword followed by a list of field names and their corresponding data types enclosed in curly braces{}
. - Fields within a struct are accessed using dot notation (
structName.fieldName
). - Structs can contain fields of different data types, including built-in types, custom types, and even other structs.
- Fields within a struct are stored in memory sequentially, and the memory layout is determined by the order of field declaration.
- Structs in Go are value types, meaning that when you assign a struct to another variable or pass it as a function argument, a copy of the struct’s value is made.
Example of a struct in Go:
package main
import "fmt"
// Defining a struct type
type Person struct {
FirstName string
LastName string
Age int
}
func main() {
// Creating a struct instance
person1 := Person{
FirstName: "John",
LastName: "Doe",
Age: 30,
}
// Accessing struct fields
fmt.Println("First Name:", person1.FirstName)
fmt.Println("Last Name:", person1.LastName)
fmt.Println("Age:", person1.Age)
}
Output :
First Name: John
Last Name: Doe
Age: 30
In this example, we define a Person
struct type with three fields: FirstName
, LastName
, and Age
. We then create an instance of the struct using the field names to initialize the values. Finally, we access the struct fields using dot notation and print them.
Structs are a fundamental building block in Go, allowing you to model complex data structures and create custom types tailored to your application’s needs.
We can access the fields of a struct in Go using the dot notation. The syntax is structName.fieldName
, where structName
is the name of the struct instance you want to access, and fieldName
is the name of the specific field within the struct.
Here’s an example of how to access fields of a struct:
package main
import "fmt"
// Defining a struct type
type Person struct {
FirstName string
LastName string
Age int
}
func main() {
// Creating a struct instance
person1 := Person{
FirstName: "John",
LastName: "Doe",
Age: 30,
}
// Accessing struct fields using dot notation
fmt.Println("First Name:", person1.FirstName)
fmt.Println("Last Name:", person1.LastName)
fmt.Println("Age:", person1.Age)
}
Output :
First Name: John
Last Name: Doe
Age: 30
In this example, the person1
instance of the Person
struct is created with the specified field values. To access the fields, we use the dot notation, such as person1.FirstName
to access the first name field.
Remember that the field names are case-sensitive, so they must match the capitalization used when defining the struct fields. Additionally, the access to fields works within the scope of the package where the struct is defined.