Go Structure


Introduction to Structs in Go

A struct in Go is a composite data type that groups together fields of different types under a single name. It is used to represent real-world entities with multiple attributes. Structs are similar to classes in object-oriented programming but do not support inheritance.


Syntax of Structs in Go

A struct is defined using the type keyword, followed by the struct name and a block containing field definitions.

package main

import "fmt"

// Defining a struct
type Car struct {
    Brand  string
    Model  string
    Year   int
    Price  float64
}

func main() {
    // Creating an instance of the  Go struct
    myCar := Car{
        Brand: "Toyota",
        Model: "Camry",
        Year:  2022,
        Price: 28000.50,
    }

    // Printing struct details
    fmt.Println("Car Details:")
    fmt.Println("Brand:", myCar.Brand)
    fmt.Println("Model:", myCar.Model)
    fmt.Println("Year:", myCar.Year)
    fmt.Println("Price:", myCar.Price)
}

Struct Initialization Methods

Go provides multiple ways to initialize a struct:

1. Using a Struct Literal (Named Fields)

p1 := Person{Name: "Alice", Age: 25}

2. Using a Struct Literal (Ordered Fields)

p2 := Person{"Bob", 30}

3. Using the new Keyword (Returns a Pointer)

p3 := new(Person)
p3.Name = "Charlie"
p3.Age = 35

Example: Struct with Methods

Methods can be associated with structs using a receiver function.

package main

import "fmt"

type Rectangle struct {
    Width  float64
    Height float64
}

// Method to calculate area
func (r Rectangle) Area() float64 {
    return r.Width * r.Height
}

func main() {
    rect := Rectangle{Width: 10.5, Height: 5.2}
    fmt.Println("Area of Rectangle:", rect.Area())
}

Pointers to Structs

Pointers allow modifying the original struct instance from a method.

package main

import "fmt"

type Counter struct {
    Value int
}

// Method to increment the value
func (c *Counter) Increment() {
    c.Value++
}

func main() {
    c := Counter{Value: 10}
    c.Increment()
    fmt.Println("Updated Counter:", c.Value)
}

Struct Embedding (Inheritance-like Behavior)

Go does not support traditional inheritance, but it allows struct embedding.

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

type Employee struct {
    Person
    Position string
}

func main() {
    e := Employee{
        Person:   Person{Name: "John", Age: 40},
        Position: "Manager",
    }

    fmt.Println("Employee Name:", e.Name)  // Accessing embedded field
    fmt.Println("Position:", e.Position)
}

Anonymous Structs

A struct can be declared without defining a separate type.

package main

import "fmt"

func main() {
    student := struct {
        Name  string
        Score int
    }{"Emma", 95}

    fmt.Println("Student Name:", student.Name)
    fmt.Println("Score:", student.Score)
}

Conclusion

Structs in Go provide a powerful way to group related data together. They allow defining methods, using pointers for modifications, and leveraging struct embedding for reusable code. They are widely used in Go applications for defining complex data structures efficiently.

Previous