Learning GoLang in One Blog

Share this post on:

Go (Golang),developed by Google, is a modern language designed for simplicity, speed, and safe concurrency.

This expanded guide covers setup, syntax, modules, testing, HTTP APIs, JSON, concurrency, and practical patterns so you can start building real services.

Why Learn Go?

  • Small language surface ® easier onboarding and consistent code reviews.
  • Static typing + fast compile times ® tighter feedback loop than many dynamic languages.
  • First class concurrency (goroutines, channels) → straightforward parallelism at scale.
  • Great tooling: go fmt, go vet, go test, go mod, go doc.
  • Cloud native pedigree: Docker, Kubernetes, Terraform, Prometheus all use Go.

Install, Initialize, Build

Install from go.dev/dl, then verify:

go version                                                 

Create a module and a starter app:

mkdir hello && cd hello
go mod init example.com/hello cat > main.go <<'EOF'
package main import "fmt"
func main() { fmt.Println("Hello, Go!") } EOF
go run .
go build -o app && ./app

Language Basics: Variables, Types, Control Flow

package main import "fmt"

func main() {
// declarations var a int = 42
b := 3.14	// type inferred const name = "Go"

// control flow
for i := 0; i < 3; i++ { fmt.Println("loop", i) }
if a > 40 { fmt.Println("big") } else { fmt.Println("small") } switch {
case a%2==0: fmt.Println("even") default: fmt.Println("odd")
}

Functions, Multiple Returns, and Errors

package main import (
"errors" "fmt" "strconv"
)

func parseInt(s string) (int, error) { i, err := strconv.Atoi(s)
if err != nil { return 0, fmt.Errorf("parse failed: %w", err) } return i, nil
}

func main() {
if v, err := parseInt("123"); err == nil { fmt.Println("ok:", v)
} else { fmt.Println("err:", err)
}
}

Structs, Methods, and Interfaces

package main import "fmt"

type User struct { Name string; Age int }

func (u User) Greet() string { return "Hi, I'm " + u.Name } type Greeter interface { Greet() string }
func Say(g Greeter) { fmt.Println(g.Greet()) }

func main() {
u := User{Name:"Asha", Age:28} Say(u)
}

Slices and Maps

nums := []int{1,2,3} nums = append(nums, 4)
m := map[string]int{"apples":3}
m["bananas"] = 5

JSON: Marshal and Unmarshal

type Product struct { ID int `json:"id"`; Name string `json:"name"` } p := Product{ID:1, Name:"Widget"}
b, _ := json.Marshal(p) // to JSON var q Product
_ = json.Unmarshal(b, &q;) // from JSON

HTTP Server in 15 Lines

package main import (
"encoding/json" "log" "net/http"
)
type Ping struct { Message string `json:"message"` } func main() {
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(Ping{Message:"pong"})
})
log.Println("listening :8080") log.Fatal(http.ListenAndServe(":8080", nil))
}

File I/O Essentials

data := []byte("hello\n")
_ = os.WriteFile("out.txt", data, 0644) b, _ := os.ReadFile("out.txt") fmt.Printf("%s", b)

Concurrency with Goroutines

func worker(id int, jobs <-chan int, results chan<- int) { for j := range jobs { results <- j*j }
}

func main() {
jobs := make(chan int, 5) results := make(chan int, 5)
for w:=1; w<=3; w++ { go worker(w, jobs, results) } for j:=1; j<=5; j++ { jobs <- j }
close(jobs)
for i:=0; i<5; i++ { fmt.Println(<-results) }
}

Channels: Synchronization & Messaging

done := make(chan struct{})
go func(){ /* work */ close(done) }()

select {
case <-done: fmt.Println("finished")
case <-time.After(time.Second): fmt.Println("timeout")
}

Testing with go test

// file: calc.go

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

// file: calc_test.go

package calc; import "testing"

func TestAdd(t *testing.T){ if Add(2,2)!=4 { t.Fatal("expected 4") } }

// run

go test ./... -v

Modules & Versioning

go mod init example.com/app

go get github.com/gorilla/mux@v1.8.1 go mod tidy

go list -m all

Conclusion

You now have a practical toolkit: language basics, modules, testing, JSON, HTTP servers, files, and robust concurrency. Use this as a starter reference for building APIs, workers, and CLIs in Go. In a follow up, we can add middleware patterns, context cancellation, graceful shutdown, and database access with sqlx or GORM.

Start experimenting with Go today. Whether you’re building APIs, CLIs, or scalable distributed systems, Go can be your go-to language for the future.

Stay tuned for Part 2, where we’ll dive into Go modules, error handling, testing, and building REST APIs

Share this post on: