RK !

Let's make comprehension easy ...

Go (Golang) Tutorial for Beginners

Author: Romaan, Last Updated: June 13, 2025, 6:41 a.m.

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

Popular Tags:


Comments: