DEV Community

Mercy Moraa
Mercy Moraa

Posted on

Getting Started with Go: A Beginner's Journey

If you told me a few months ago that I would be obsessed with pointers, memory allocation, and low-level strings manipulation, I probably would have laughed. But here I am, deep into a rigorous technical software development program, and completely falling in love with Go (Golang).

Go was designed by Google to be simple, blazing fast, and highly concurrent. While it handles heavy backend architectures globally, it is also one of the most rewarding languages to learn as a beginner, no matter where you are in the world.

In this article, I want to share my raw insights from an intense tech training environment, unpack Go's fundamental layout, and help you build your very first Command Line Interface (CLI) application using nothing but the Go Standard Library. No cheat frameworks. No heavy external dependencies. Just raw logic.

Why Go?

When you start learning Go in a strict environment, you quickly realize it doesn't hold your hand like Python, but it isn't as terrifyingly manual as C. It sits in the perfect sweet spot:

  • Strict Compilation: If you import a package or declare a variable and don't use it, your code will not compile. This forces you to write clean, minimal code from day one.
  • Blazing Fast: It compiles directly to machine code, which means it runs incredibly fast.
  • Standard Library Power: Go’s built-in tools (net/http, os, strconv) are so powerful that you rarely need to download external packages to build production-grade tools.

Step 1: Setting Up Your Workspace

Before writing code, initialize your project workspace. Open your terminal and run:

mkdir go-beginner-cli
cd go-beginner-cli
go mod init go-beginner-cli

Enter fullscreen mode Exit fullscreen mode

This creates a go.mod file, which manages your application's path and dependencies.

Step 2: Understanding the Base Structure

Every execution file in Go follows a strict anatomy. Create a main.go file and inspect this template:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, World!")
}

Enter fullscreen mode Exit fullscreen mode

The Breakdown:

  1. package main: Tells the Go compiler that this file should compile as an executable program rather than a shared utility library.
  2. import: Brings in standard tools. Here, "fmt" (Format) handles console input and output.
  3. func main(): The ultimate entry point. When you run your program, execution starts exactly here.

To execute it, run:

go run main.go

Enter fullscreen mode Exit fullscreen mode

Step 3: Building Your First App ; A Dynamic CLI Case Converter

Let’s move past "Hello World" and build a practical CLI application. We will write a tool that accepts a string argument from the terminal and modifies it based on what the user wants (Uppercase or Lowercase).

We will use the native os package to capture terminal input arguments (os.Args).

Replace the contents of main.go with the following code:

package main

import (
    "fmt"
    "os"
    "strings"
)

func main() {
    // os.Args[0] is always the program name itself.
    // We expect: program_name, string_to_modify, and mode (up/low)
    if len(os.Args) < 3 {
        fmt.Println("Usage: go run . [text] [up|low]")
        return
    }

    inputText := os.Args[1]
    mode := os.Args[2]

    switch mode {
    case "up":
        result := strings.ToUpper(inputText)
        fmt.Println("Result:", result)
    case "low":
        result := strings.ToLower(inputText)
        fmt.Println("Result:", result)
    default:
        fmt.Println("Unknown mode! Use 'up' for uppercase or 'low' for lowercase.")
    }
}

Enter fullscreen mode Exit fullscreen mode

How to Run Your New CLI Tool:

Test your application directly from your terminal pane by passing arguments:

Test 1: Uppercase Mod

go run . "learning go is awesome" up
# Output: Result: LEARNING GO IS AWESOME

Enter fullscreen mode Exit fullscreen mode

Test 2: Lowercase Mod

go run . "GLOBAL DEVELOPERS" low
# Output: Result: global developers

Enter fullscreen mode Exit fullscreen mode

Key Takeaways from My Learning Journey

Building text manipulation tools and algorithmic blueprints from scratch taught me three vital lessons:

  1. Think in Runes, Not Just Strings: In Go, strings are read-only slices of bytes. If you want to handle text properly without breaking special characters, learn to convert your data into []rune.
  2. Handle Errors Safely: Go doesn't use traditional try/catch exceptions. Functions return data and errors explicitly side-by-side. It forces you to deal with errors immediately.
  3. Keep it Simple: The beauty of Go lies in its minimalism. If your function is getting too complex, break it down into smaller structural files under package main.

Wrap Up

This is just the baseline of what you can accomplish. Once you master string tracking and the basic os filesystem controls, you can transition into building full file processors, custom parsers, or high-performance APIs.

Are you currently learning Go or thinking about diving in? Let’s connect in the comments below! Share your favorite Go optimization tips or ask any questions if you are stuck on your own learning pipeline.

Happy coding!

Top comments (0)