The RK Times
← All posts

Go (Golang) Tutorial for Beginners

By Romaan · Jun 13, 2025 · 2 min read · 53 views

Go is a high-level general purpose programming language that is statically typed and compiled. It is known for the simplicity of its syntax and the efficiency of development that it enables by the inclusion of a large standard library supplying many needs for common projects.

 

Build Your First Go Application Step-by-Step

Go (or Golang), developed at Google, is a statically typed, compiled language known for its simplicity, performance, and built-in support for concurrency. It’s a great choice for backend services, cloud tools, and scalable systems.

1. Install and Set Up Go

📥 Install Go

Visit: https://go.dev/dl/

🧪 Verify Installation

go version

You should see something like:

go version go1.22.0 darwin/amd64

📁 Setup Your First Project

mkdir hello-go && cd hello-go
go mod init hello-go

✍️ 2. Go Basics

🔸 Hello World

package main

import "fmt"

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

Run it:

go run main.go

🔸 Variables

var name string = "Romaan"
age := 30
fmt.Printf("Name: %s, Age: %d\n", name, age)

🔸 Functions

func add(a int, b int) int {
    return a + b
}

🔸 Conditionals

if age > 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Minor")
}

🔸 Loops

for i := 1; i <= 5; i++ {
    fmt.Println(i)
}

🔸 Arrays and Slices

var nums [3]int = [3]int{1, 2, 3}
names := []string{"Alice", "Bob"}
names = append(names, "Charlie")

🔸 Maps

user := map[string]string{
    "name": "Romaan",
    "role": "Engineer",
}
fmt.Println(user["name"])

3. Build a Simple CLI App

Create a file named main.go:

package main

import (
    "fmt"
    "os"
    "strconv"
)

func main() {
    if len(os.Args) < 3 {
        fmt.Println("Usage: go run main.go <num1> <num2>")
        return
    }

    a, _ := strconv.Atoi(os.Args[1])
    b, _ := strconv.Atoi(os.Args[2])
    result := a + b

    fmt.Printf("Sum: %d\n", result)
}

Run it:

go run main.go 4 5

4. Working with Packages

Create mathutils/math.go:

package mathutils

func Add(a, b int) int {
    return a + b
}

Use it in main.go:

package main

import (
    "fmt"
    "hello-go/mathutils"
)

func main() {
    fmt.Println(mathutils.Add(2, 3))
}

5. Unit Testing

Create mathutils/math_test.go:

package mathutils

import "testing"

func TestAdd(t *testing.T) {
    got := Add(2, 3)
    want := 5

    if got != want {
        t.Errorf("Add(2, 3) = %d; want %d", got, want)
    }
}

Run the test:

go test ./...

6. What Next?

  • Build REST APIs with Gin
  • Explore goroutines and channels
  • Master Go’s standard libraries
  • Learn about struct composition and interfaces

Resources


Comments (0)

Be the first to comment.