Pointers, Structs, and Methods in Go (Golang)

Pointers, Structs, and Methods in Go (Golang)

Yeasir Arafat Yeasir Arafat | 2 min read
2 months ago

1. Pointers

What is a Pointer?

A pointer holds the memory address of a value. Use & to get the address and * to access the value at the address.

Example:

func main() {
  x := 10
  ptr := &x          // ptr stores the memory address of x
  fmt.Println(ptr)   // Output: 0xc000018030 (example address)
  fmt.Println(*ptr)  // Output: 10 (value at the address)

  *ptr = 20          // Modify x through the pointer
  fmt.Println(x)     // Output: 20
}
  • Why Use Pointers?
    • To modify the original variable inside a function.
    • To optimize memory (avoid copying large structs).

2. Structs

What is a Struct?

A struct is a typed collection of fields (like a "class" in other languages).

Define a Struct:

type User struct {
  ID        int
  FirstName string
  LastName  string
  Email     string
}

Create a Struct Instance:

func main() {
  // Method 1: Declare fields explicitly
  user1 := User{
    ID:        1,
    FirstName: "John",
    LastName:  "Doe",
    Email:     "[email protected]",
  }

  // Method 2: Short declaration (order matters!)
  user2 := User{2, "Alice", "Smith", "[email protected]"}

  fmt.Println(user1.FirstName) // Output: John
}

3. Methods

What is a Method?

A method is a function with a receiver (attached to a struct type).

Define a Method:

// Value receiver (works on a copy of the struct)
func (u User) FullName() string {
  return u.FirstName + " " + u.LastName
}

// Pointer receiver (modifies the original struct)
func (u *User) UpdateEmail(newEmail string) {
  u.Email = newEmail
}

func main() {
  user := User{1, "John", "Doe", "[email protected]"}
  fmt.Println(user.FullName()) // Output: John Doe

  user.UpdateEmail("[email protected]")
  fmt.Println(user.Email) // Output: [email protected]
}
  • Key Points:
    • Use pointer receivers (*User) to modify the original struct.
    • Use value receivers (User) for read-only operations.

4. Embedded Structs (Composition)

Go uses composition over inheritance. Embed one struct into another:

type Address struct {
  City  string
  State string
}

type Employee struct {
  User    // Embedded struct (inherits User's fields/methods)
  Address // Embedded Address
  Salary  float64
}

func main() {
  emp := Employee{
    User:    User{1, "Jane", "Doe", "[email protected]"},
    Address: Address{"New York", "NY"},
    Salary:  75000,
  }

  fmt.Println(emp.FirstName)    // Access embedded User field
  fmt.Println(emp.Address.City) // Output: New York
}

Practice Exercise

  1. Create a Book struct with fields Title, Author, and Pages.
  2. Add a method IsLong() that returns true if the book has > 300 pages.
  3. Use a pointer receiver to update the book’s title.

Next Steps

Let me know if you want to:

  • Dive deeper into interfaces and type assertions.
  • Explore concurrency (goroutines/channels).
  • Start building a simple HTTP server with Go’s net/http package.


Discussions

Login to Post Comments
No Comments Posted