EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Go Programming Language

Structures in Golang

structure, or struct, in Go is a user-defined data type that groups together related items of different types into a single type. Any real-world entity with a set of properties or attributes can be effectively represented as a struct. This concept is often compared to classes in object-oriented programming but serves as a lightweight alternative. Unlike classes, structs in Go do not support inheritance but do support composition. For instance, consider a Person having a name, age, and address. It makes sense to group these attributes into a single structure like this:

Declaring a Structure:

type Person struct {
    name    string
    age     int
    address string
}

Here, the type keyword is used to introduce a new type named Person, followed by the keyword struct, indicating that we are defining a structure. Inside the curly braces {}, the struct fields are listed with their names and corresponding data types.

Compact Notation:

Fields of the same type can be declared together, as shown in this example:

type Person struct {
    name, address string
    age           int
}

Declaring and Initializing a Structure:

To define a variable of a struct type, use the following syntax:

var p Person

This creates a variable p of type Person, initializing all fields to their zero values ("" for strings, 0 for integers, etc.). A struct can also be initialized with values using a struct literal:

var p = Person{"Alice", 25, "New York"}

Named Field Initialization:

The name:value syntax allows initializing fields by name, skipping the order requirement:

var p = Person{name: "Bob", age: 30}

Uninitialized fields will automatically be set to their zero values.

Example Program:

package main

import "fmt"

// Define a struct
type Person struct {
    name    string
    age     int
    address string
}

func main() {
    // Zero-value initialization
    var p Person
    fmt.Println(p)

    // Initializing with struct literal
    p1 := Person{"Alice", 25, "New York"}
    fmt.Println("Person1:", p1)

    // Named initialization
    p2 := Person{name: "Bob", age: 30}
    fmt.Println("Person2:", p2)

    // Uninitialized fields have zero value
    p3 := Person{name: "Charlie"}
    fmt.Println("Person3:", p3)
}

Output:

{ 0 }
Person1: {Alice 25 New York}
Person2: {Bob 30 }
Person3: {Charlie 0 }

Accessing Fields of a Struct:

Fields of a struct are accessed using the dot (.) operator.

Example Program:

package main

import "fmt"

// Define the struct
type Vehicle struct {
    brand, model string
    weight       float64
}

func main() {
    v := Vehicle{brand: "Tesla", model: "Model S", weight: 2100}

    // Access and display fields
    fmt.Println("Vehicle Brand:", v.brand)
    fmt.Println("Vehicle Model:", v.model)

    // Update a field value
    v.model = "Model X"
    fmt.Println("Updated Vehicle:", v)
}

Output:

Vehicle Brand: Tesla
Vehicle Model: Model S
Updated Vehicle: {Tesla Model X 2100}

Pointers to a Struct:

Pointers in Go store the memory address of variables. Similarly, you can create pointers to structs.

Example Program:

package main

import "fmt"

// Define a struct
type Book struct {
    title, author string
    price         float64
}

func main() {
    b := &Book{"Go Programming", "John Doe", 29.99}

    // Access fields via pointer
    fmt.Println("Title:", b.title)
    fmt.Println("Author:", b.author)
}

Output:

Title: Go Programming
Author: John Doe

Additional Details and Benefits:

  1. Encapsulation: Structures group related data, simplifying management.
  2. Code Organization: They help organize complex data logically.
  3. Type Safety: Define types for fields, reducing type-related errors.
  4. Flexibility and Performance: Structures are efficient for memory and processing.
Before: a = 10, b = 20
Sum: 35
After: a = 10, b = 20
End of lesson.