Let’s Go Further! A Beginner’s Guide to Go (Golang) — Part 2

Ritupandey
3 min readAug 12, 2023

--

Welcome back, curious learners, to the next chapter of our exciting journey through Go (Golang). If you haven’t already, catch up on Part 1 here, where we got our feet wet with the basics of Go.

Part 2: Unveiling More Golang Magic

In Part 1, we laid the foundation of Go by understanding the essentials. Now, it’s time to dig deeper and uncover some more magic that this language has to offer. Let’s dive right in!

1. Loops: Repeating with Ease

Think of loops as your “do this again” tools. They’re like the loops on your favorite roller coaster — they make things happen repeatedly. Here’s a simple for loop:

package main

import "fmt"

func main() {
for i := 1; i <= 5; i++ {
fmt.Println("Count:", i)
}
}

This code prints numbers from 1 to 5 using a loop. Easy, right?

2. Arrays and Slices: Data Containers

Imagine arrays and slices as your lunchboxes. You put different snacks in each compartment, and they keep things organized. Here’s a glimpse:

package main

import "fmt"

func main() {
numbers := []int{1, 2, 3, 4, 5}
fmt.Println(numbers[2]) // Prints the third snack (3)
}

This snippet shows a slice of numbers and how to grab a specific one from it.

3. Packages: Your Tool Friends

Packages are like toolboxes with ready-to-use tools. We’ve met some packages already, like the fmt toolbox that helps you with printing. Here's another example:

package main

import (
"fmt"
"math/rand"
)

func main() {
fmt.Println("Random number:", rand.Intn(100))
}

In this snippet, we use the rand toolbox to generate a random number.

4. Pointers: Your Map to Memory

Pointers are like maps that show you where your stuff is stored in memory. They’re super useful for some advanced tasks, like finding hidden treasures. Here’s a little peek:

package main

import "fmt"

func main() {
num := 42
ptr := &num // This points to where 42 is stored

fmt.Println("Value of num:", num)
fmt.Println("Value pointed by ptr:", *ptr)
}

This code introduces pointers by showing how they can point to a value.

5. Structs: Organized Data

Structs are like backpacks with different pockets for different things. They help you organize your stuff better. Check out this sneak peek:

package main

import "fmt"

type Person struct {
Name string
Age int
Hobby string
}

func main() {
person := Person{"Alice", 25, "Hiking"}
fmt.Println(person)
}

Here, we define a Person struct with different fields and create a person with specific details.

6. Error Handling: Graceful Recovery

Mistakes happen, even to the best of us. Error handling is like wearing a helmet while riding a bike — it keeps you safe. Here’s a simple example:

package main

import (
"fmt"
"errors"
)

func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero is not allowed")
}
return a / b, nil
}

func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result)
}

This code snippet demonstrates how to handle errors gracefully using the errors package.

Parting Wisdom

Congratulations, you’ve reached the end of Part 2! We’ve journeyed through loops, arrays, slices, packages, pointers, structs, and error handling — all in beginner-friendly terms. You’re building a strong foundation in Go, one concept at a time.

Stay tuned for Part 3, where we’ll unravel more fascinating Go features. Remember, practice makes perfect! Don’t hesitate to review Part 1 and share your thoughts in the comments below.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ritupandey
Ritupandey

Written by Ritupandey

Currently an undergrad student doing IT majors, primarily interested in data analytics and marketing.

No responses yet

Write a response