Go container/heap 🎯

beginner
21 min

Go container/heap 🎯

Welcome to our deep dive into Go's container and heap! This lesson is designed to help beginners and intermediate learners understand the core concepts, real-world examples, and practical applications of these essential data structures. Let's get started!

What are containers and heap in Go? 📝

In Go, containers and heap are fundamental data structures that help manage memory allocation and deallocation for variables.

  • Container: Go uses several containers such as arrays, slices, maps, and structs to store data. The built-in make() function is used to create containers in Go.

  • Heap: The heap is an area of memory used by Go to dynamically allocate and deallocate memory for variables at runtime. The built-in new() function is used to allocate memory from the heap.

How does Go manage memory? 💡

Go uses a garbage collector to automatically manage memory allocation and deallocation. The garbage collector runs periodically to identify and free up memory that is no longer in use.

Container types in Go 📝

Arrays

An array is a fixed-size, homogeneous data structure. It stores elements of the same type in contiguous memory locations.

go
arr := [5]int{1, 2, 3, 4, 5} // Declaring an array with initial values

Slices

A slice is a flexible, resizable, and heterogeneous data structure. It provides a view of a contiguous portion of an array, allowing elements to be added, removed, or modified as needed.

go
slice := []int{1, 2, 3, 4, 5} // Declaring a slice

Maps

A map is a key-value pair data structure that stores unique keys and their corresponding values.

go
map1 := map[string]int{"apple": 1, "banana": 2} // Declaring a map

Structs

A struct is a composite data structure that groups related data items together.

go
type Person struct { Name string Age int } person := Person{"John", 25} // Declaring and initializing a struct

Heap and memory allocation 💡

When you create a variable using the new() function, Go allocates memory for that variable from the heap.

go
var p *Person = new(Person) // Allocating memory for a struct from the heap

Cleaning up memory 💡

Go's garbage collector automatically frees up memory that is no longer in use. However, if you explicitly want to free up memory, you can use the delete() function.

go
delete(map1, "apple") // Deleting a key-value pair from a map

Quiz 🎯

Quick Quiz
Question 1 of 1

Which built-in function is used to create a slice in Go?

Practical Example 🎯

Let's create a simple program that demonstrates slices and memory allocation using the heap.

go
package main import "fmt" type Person struct { Name string Age int } func main() { // Creating a slice with initial values people := []Person{{Name: "John", Age: 25}, {Name: "Jane", Age: 22}} // Allocating memory for a new Person from the heap var newPerson *Person = new(Person) newPerson.Name = "Alice" newPerson.Age = 28 // Adding the new Person to the slice people = append(people, *newPerson) // Printing the slice for _, person := range people { fmt.Println(person.Name, person.Age) } }

This program creates a slice with initial values, allocates memory for a new Person using the heap, adds the new Person to the slice, and then prints the contents of the slice.

And that's a wrap! Now you have a solid understanding of Go's container and heap, as well as how to use them in practice. Keep learning and experimenting, and happy coding! 🚀