Go Pointers to Structs šŸŽÆ

beginner
12 min

Go Pointers to Structs šŸŽÆ

Welcome back to CodeYourCraft! Today, we're diving into a fascinating aspect of Go programming: Pointers to Structs. By the end of this lesson, you'll be able to create and manipulate complex data structures using pointers. Let's get started!

Understanding Pointers šŸ“

Before we dive into structs, let's take a moment to review pointers. A pointer in Go is a variable that stores the memory address of another variable. Pointers allow us to work with variables directly and are crucial when dealing with structs.

go
var myInt *int = new(int) // Creating a pointer to an integer

šŸ’” Pro Tip: Use the new() function to allocate memory for pointers.

Structures šŸ“

Structures, or structs, are a collection of fields (variables) of different types. They allow us to create complex data types that can represent real-world objects.

go
type Person struct { Name string Age int Address string }

šŸ’” Pro Tip: Use the type keyword to define a struct, and specify the type, variables, and their data types within curly braces.

Pointers to Structs šŸ’”

Now that we understand both pointers and structs, let's learn how to combine them. Pointers to structs are used when we want to modify the original struct, rather than creating a new copy.

go
func main() { person := new(Person) // Creating a new Person struct and getting its address person.Name = "John Doe" person.Age = 30 person.Address = "123 Main Street" printPerson(person) // Passing the struct's address to the printPerson function } func printPerson(p *Person) { // Using a pointer to print the struct fmt.Println("Name:", p.Name) fmt.Println("Age:", p.Age) fmt.Println("Address:", p.Address) }

šŸ’” Pro Tip: Pass the struct's address to functions that require a pointer to the struct, and use the *Person type in the function definition to accept the pointer.

Practical Example šŸ“

Let's take a look at a practical example where we create a simple database of people using structs and pointers.

go
type Person struct { Name string Age int Address string } func main() { people := make([]*Person, 3) // Creating an array of pointers to Person structs people[0] = new(Person) people[0].Name = "John Doe" people[0].Age = 30 people[0].Address = "123 Main Street" people[1] = new(Person) people[1].Name = "Jane Smith" people[1].Age = 28 people[1].Address = "456 Oak Street" people[2] = new(Person) people[2].Name = "Alice Johnson" people[2].Age = 35 people[2].Address = "789 Pine Lane" printPeople(people) // Printing all people in the database } func printPeople(people []*Person) { // Function to print all people in the database for _, person := range people { fmt.Println("Name:", person.Name) fmt.Println("Age:", person.Age) fmt.Println("Address:", person.Address) fmt.Println("-------------------") } }

šŸ’” Pro Tip: Use the make() function to allocate memory for arrays, and don't forget to initialize each person in the array.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What does the `*` symbol represent in Go?

And that's it for today's lesson! Pointers to structs are a powerful tool in Go, and with practice, you'll become proficient in using them to manipulate complex data structures. Keep coding and learning with CodeYourCraft! šŸš€šŸŒŸ