How to Use Structs In Go?

12 minutes read

Structs in Go are a way to define and encapsulate a collection of fields into a single named entity. They are similar to classes in object-oriented programming languages. Structs allow you to create complex data structures by combining different types of fields.


To define a struct, you use the type keyword followed by the name of the struct and the keyword struct. Inside the struct, you list the fields, where each field has a name and a type.


For example, let's say we want to define a struct for a person:

1
2
3
4
type Person struct {
    Name string
    Age  int
}


In the above example, we defined a struct named Person with two fields: Name of type string and Age of type int.


To create a new instance of a struct, you can use the struct literal syntax. This involves specifying the name of the struct followed by the values for each field within curly braces.

1
2
3
4
person := Person{
    Name: "John",
    Age:  30,
}


You can access the fields of a struct using dot notation. For example, to access the Name field of the person struct, you can use person.Name.


Structs can also be used as method receivers, allowing you to associate behavior with the struct. Methods can be defined using the func keyword followed by the receiver parameter and the function body.

1
2
3
func (p Person) SayHello() {
    fmt.Println("Hello, my name is", p.Name)
}


In the above example, we defined a method SayHello() for the Person struct, which prints a greeting along with the person's name.


Structs can be further enhanced by embedding other structs, enabling composition and code reusability. This is achieved by simply adding the name of the struct type as a field within another struct.

1
2
3
4
type Employee struct {
    Person
    Salary float64
}


In the above example, the Employee struct embeds the Person struct, making all the fields and methods of Person available to Employee as if they were defined directly within Employee.


Overall, structs in Go are a powerful way to define and work with custom data types, providing a flexible and efficient means of organizing and manipulating data.

Best Golang Books to Read in 2024

1
Learning Go: An Idiomatic Approach to Real-World Go Programming

Rating is 5 out of 5

Learning Go: An Idiomatic Approach to Real-World Go Programming

2
Distributed Services with Go: Your Guide to Reliable, Scalable, and Maintainable Systems

Rating is 4.9 out of 5

Distributed Services with Go: Your Guide to Reliable, Scalable, and Maintainable Systems

3
Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools

Rating is 4.8 out of 5

Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools

4
Event-Driven Architecture in Golang: Building complex systems with asynchronicity and eventual consistency

Rating is 4.7 out of 5

Event-Driven Architecture in Golang: Building complex systems with asynchronicity and eventual consistency

5
Go Programming Language, The (Addison-Wesley Professional Computing Series)

Rating is 4.6 out of 5

Go Programming Language, The (Addison-Wesley Professional Computing Series)

6
Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures, 2nd Edition

Rating is 4.5 out of 5

Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures, 2nd Edition

7
Hands-On Software Architecture with Golang: Design and architect highly scalable and robust applications using Go

Rating is 4.4 out of 5

Hands-On Software Architecture with Golang: Design and architect highly scalable and robust applications using Go

8
Head First Go

Rating is 4.3 out of 5

Head First Go


How to pass a struct to a function in Go?

To pass a struct to a function in Go, you can do the following:

  1. Define the struct with its fields and values.
  2. Declare a function that accepts the struct as a parameter.
  3. Call the function and pass the struct as an argument.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package main

import "fmt"

type Person struct {
    name string
    age  int
}

func updateName(p *Person, newName string) {
    p.name = newName
}

func main() {
    person := Person{name: "John", age: 30}
    fmt.Println("Before update:", person)

    updateName(&person, "Jane")
    fmt.Println("After update:", person)
}


In this example, we have a struct Person with two fields: name and age. We define a function updateName that takes a pointer to a Person struct and a new name as parameters. The function updates the name field of the struct with the new name.


In the main function, we create a person instance with the initial name "John". We then call the updateName function and pass the address of the person struct using the & operator. This allows the function to modify the name field of the struct.


The output of the program will be:

1
2
Before update: {John 30}
After update: {Jane 30}


As you can see, passing a struct to a function in Go involves passing a pointer to the struct. This allows the function to modify the struct's fields directly.


How to define nested anonymous structs in Go?

In Go, you cannot define nested anonymous structs directly. However, you can define an anonymous struct as a field of another struct.


Here's an example of how you can define nested anonymous structs in Go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main

import "fmt"

type Employee struct {
    ID   int
    Name string
    Contact struct {
        Phone   string
        Email   string
        Address struct {
            Street string
            City   string
        }
    }
}

func main() {
    employee := Employee{
        ID:   1,
        Name: "John Doe",
        Contact: struct {
            Phone   string
            Email   string
            Address struct {
                Street string
                City   string
            }
        }{
            Phone: "1234567890",
            Email: "johndoe@example.com",
            Address: struct {
                Street string
                City   string
            }{
                Street: "123 Main St",
                City:   "New York",
            },
        },
    }

    fmt.Println("Employee ID:", employee.ID)
    fmt.Println("Employee Name:", employee.Name)
    fmt.Println("Phone:", employee.Contact.Phone)
    fmt.Println("Email:", employee.Contact.Email)
    fmt.Println("Address Street:", employee.Contact.Address.Street)
    fmt.Println("Address City:", employee.Contact.Address.City)
}


In this example, the Employee struct contains an anonymous struct Contact, which in turn contains another anonymous struct Address. The fields of these anonymous structs are defined within the struct literals when the struct is instantiated.


Note that each anonymous struct definition is unique even if the fields are repeated.


How to handle struct fields with different visibility in Go?

In Go, you can handle struct fields with different visibility by using a combination of exporting and unexporting the fields.

  1. Exported fields: Fields that start with an uppercase letter are considered exported or public fields. These fields can be accessed from any package that imports the struct.


Example:

1
2
3
4
type MyStruct struct {
    PublicField  int    // Exported or public field
    privateField string // Unexported or private field
}


  1. Unexported fields: Fields that start with a lowercase letter are considered unexported or private fields. These fields can only be accessed within the same package.


To access or modify unexported fields of a struct from another package, you can provide methods or functions in the struct's package that interact with those fields.


Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package mypackage

type MyStruct struct {
    privateField string // Unexported or private field
}

func (s *MyStruct) GetPrivateField() string {
    return s.privateField
}

func (s *MyStruct) SetPrivateField(val string) {
    s.privateField = val
}


Now, from a different package, you can use the methods to interact with the unexported field:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "mypackage"

func main() {
    s := mypackage.MyStruct{}
    s.SetPrivateField("new value")
    privateFieldValue := s.GetPrivateField()
    // Use the privateFieldValue
}


By using methods or functions, you can control the access to the unexported fields and provide safe and controlled access from other packages.


What are nested structs in Go?

In Go, a nested struct refers to a struct type that is defined within another struct. It allows you to organize and encapsulate related data within a single structure.


Here is an example of a nested struct in Go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
type Person struct {
    Name  string
    Age   int
    Address struct {
        Street  string
        City    string
        Country string
    }
}

func main() {
    p := Person{
        Name: "John Doe",
        Age:  30,
        Address: struct {
            Street  string
            City    string
            Country string
        }{
            Street:  "123 Main St",
            City:    "New York",
            Country: "USA",
        },
    }

    fmt.Println(p.Name)
    fmt.Println(p.Address.Street)
    fmt.Println(p.Address.City)
    fmt.Println(p.Address.Country)
}


In this example, the Address field of the Person struct is a nested struct. It has three fields: Street, City, and Country. To access the nested struct's fields, you use the dot notation (p.Address.Street, p.Address.City, p.Address.Country).


Nested structs are useful when you have related data that belongs to a specific context (such as address details for a person) and want to keep them organized within a parent structure.


What is the difference between struct and map in Go?

In Go, struct and map are two different data types used for different purposes:

  1. struct: A struct is a composite data type that groups together zero or more values with different types into a single entity. It is often used to represent a collection of related fields. struct fields are accessed using dot notation. For example:
1
2
3
4
5
6
7
type Person struct {
    Name string
    Age  int
}

p := Person{Name: "Alice", Age: 25}
fmt.Println(p.Name, p.Age) // Output: Alice 25


  1. map: A map is a built-in type that represents an unordered collection of key-value pairs. It is implemented as a hash table and provides fast lookup, insertion, and deletion operations based on keys. map is dynamic in size and grows as new elements are added. Keys must be of a hashable type and values can be of any type. For example:
1
2
3
4
m := make(map[string]int)
m["one"] = 1
m["two"] = 2
fmt.Println(m["two"]) // Output: 2


In summary, struct is used for defining structured data types with specific fields, while map is used for storing unordered key-value pairs for efficient lookup.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

In Swift, a struct is a custom data type that allows you to group together related properties and functions. To create a struct, you use the "struct" keyword followed by the name of the struct and its properties. You can define variables and constants ...
To use foreach on an array of struct in Swift, you can iterate over each element of the array using a for-in loop.For example, if you have an array of structs called students: struct Student { var name: String var age: Int } let students = [Student(na...
To access an item of a struct in Swift, you can use dot notation followed by the name of the item within the struct. For example, if you have a struct called Person with a property name, you can access the name property of an instance of Person like this: stru...