Introduction to Go (Golang)
Go, also known as Golang, is a modern programming language developed at Google to solve real-world problems in large-scale systems.
It was designed to be:
- Simple
- Fast
- Efficient
- Scalable
Go is widely used for:
- Backend development (APIs, servers)
- Cloud infrastructure
- DevOps tools
- Networking systems
Popular tools built in Go:
- Docker
- Kubernetes
- Terraform
Why Go is So Popular
🔹 Key Benefits
- Fast Compilation Strongly Typed & Statically Typed
var age int = 16
Wrong:age = "hello" // ERROR
Checked at compile time → fewer bugs
Memory Management (Garbage Collector)
Go uses:
Automatic Garbage Collection
Unlike C:
No manual memory free
Safer
Conditionals in Goif age > 18 {Special Feature:
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}if x := 10; x > 5 {
fmt.Println(x)
}
Arrays vs Slices
Array (Fixed)var arr [3]int = [3]int{1,2,3}
Slice (Dynamic)nums := []int{1,2,3}
Slice Operationsnums = append(nums, 4)
fmt.Println(len(nums))
fmt.Println(cap(nums))
Important Rulenums = append(nums, 5) // ALWAYS reassign
Maps in Gom := make(map[string]int)
m["age"] = 16
Safe Access:value, ok := m["age"]
Common Crashvar m map[string]int
m["x"] = 1 // PANIC
Fix:m = make(map[string]int)
Pointers in Gox := 10
p := &x
fmt.Println(*p)
Crash Examplevar p *int
fmt.Println(*p) // PANIC
Fix:if p != nil {
fmt.Println(*p)
}
Interfaces in Go
type Shape interface {
Area() float64
}
✔ No inheritance
✔ Simple design
✔ Implicit implementation - Variadic Functions
func sum(nums ...int) int {
total := 0
for _, v := range nums {
total += v
}
return total
}
Concurrency in Go (Powerful Feature)
Goroutines
go myFunction()
Channelsch := make(chan int)
ch <- 5
value := <-ch
Select Statementselect {
case msg := <-ch1:
fmt.Println(msg)
case msg := <-ch2:
fmt.Println(msg)
}
Channel Crash Rules
Operation
Result
Send to nil
Block forever
Send to closed
Panic
Receive closed
Zero value
WaitGroup Examplevar wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Done")
}()
wg.Wait()
Go Modulesgo mod init myproject
go mod tidy
Backend Development Examplehttp.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello API")
})
http.ListenAndServe(":8080", nil)
Common Errors & Fixes
Panic: nil pointer
✔ Fix: check before use
Slice bug (append)
✔ Fix:slice = append(slice, val)
Deadlockch := make(chan int)
ch <- 1 // no receiver → deadlock
✔ Fix:go func(){ ch <- 1 }()
<-ch
Import errors
✔ Fix:go mod tidy
Advantages of Go
✔ Fast compilation
✔ Simple syntax
✔ Built-in concurrency
✔ No VM required
✔ Cross-platform binaries
✔ Strong typing
x Disadvantages of Go
xNo generics (older versions)
x Limited OOP features
x Verbose error handling
x Garbage collector overhead
Fun Facts About Go
Go was created in 2009
Designed by engineers at Google
Compilation is so fast it feels like scripting
Used in massive systems like Kubernetes
Has built-in concurrency (rare in languages)- Much faster than C, C++, Rust
- Saves developer time
- Good Performance
- Faster than Python/JavaScript
- Comparable to Java/C#
- Low Memory Usage
- No heavy virtual machine (like Java)
- Lightweight runtime
- Simple Syntax
- Easier than C++
- Cleaner than Java
Is Go a Compiler or Interpreter?
Go is a compiled language
What is Compilation?
Compilation = converting human-readable code into machine code
Example:
fmt.Println("Hello World")
Becomes:
Binary executable → runs directly on CPU
Go vs Interpreted Languages
| Feature | Go | Python |
|---|---|---|
| Type | Compiled | Interpreted |
| Speed | Fast | Slower |
| Needs runtime? | ❌ No | ✅ Yes |
How Compilation Works in Go
Build Command
go build main.go
Output:
main.exe (Windows)
main (Linux/Mac)
Run Without Building
go run main.go
Installing Go on All Platforms
Windows (64-bit & 32-bit)
- Go to official website
- Download:
- Install and verify:
go version
Common Windows Issues
“go not recognized”
Fix:
- Add Go to PATH:
C:\Program Files\Go\bin
macOS
Using Homebrew:
brew install go
Or download pkg installer.
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install golang
Or manual:
wget https://go.dev/dl/go1.xx.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf file.tar.gz
Add to PATH:
export PATH=$PATH:/usr/local/go/bin
Termux (Android)
pkg update
pkg install golang
Check:
go version
Basic Go Program
package mainimport "fmt"func main() {
fmt.Println("Hello, Mustafa!")
}
Run:
go run main.go
Strongly Typed & Statically Typed
var age int = 16
x Wrong:
age = "hello" // ERROR
✔ Checked at compile time → fewer bugs
Memory Management (Garbage Collector)
Go uses:
Automatic Garbage Collection
Unlike C:
- No manual memory free
- Safer
Conditionals in Go
if age > 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
Special Feature:
if x := 10; x > 5 {
fmt.Println(x)
}
Arrays vs Slices
Array (Fixed)
var arr [3]int = [3]int{1,2,3}
Slice (Dynamic)
nums := []int{1,2,3}
Slice Operations
nums = append(nums, 4)
fmt.Println(len(nums))
fmt.Println(cap(nums))
Important Rule
nums = append(nums, 5) // ALWAYS reassign
Maps in Go
m := make(map[string]int)
m["age"] = 16
Safe Access:
value, ok := m["age"]
x Common Crash
var m map[string]int
m["x"] = 1 // PANIC
Fix:
m = make(map[string]int)
Pointers in Go
x := 10
p := &x
fmt.Println(*p)
Crash Example
var p *int
fmt.Println(*p) // PANIC
Fix:
if p != nil {
fmt.Println(*p)
}
Interfaces in Go
type Shape interface {
Area() float64
}
✔ No inheritance
✔ Simple design
✔ Implicit implementation
Variadic Functions
func sum(nums ...int) int {
total := 0
for _, v := range nums {
total += v
}
return total
}
Concurrency in Go (Powerful Feature)
Goroutines
go myFunction()
Channels
ch := make(chan int)
ch <- 5
value := <-ch
Select Statement
select {
case msg := <-ch1:
fmt.Println(msg)
case msg := <-ch2:
fmt.Println(msg)
}
Channel Crash Rules
| Operation | Result |
|---|---|
| Send to nil | Block forever |
| Send to closed | Panic |
| Receive closed | Zero value |
Wait Group Example
var wg sync.WaitGroupwg.Add(1)
go func() {
defer wg.Done()
fmt.Println("Done")
}()
wg.Wait()
Go Modules
go mod init myproject
go mod tidy
Backend Development Example
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello API")
})
http.ListenAndServe(":8080", nil)
Common Errors & Fixes
x Panic: nil pointer
✔ Fix: check before use
x Slice bug (append)
✔ Fix:
slice = append(slice, val)
x Deadlock
ch := make(chan int)
ch <- 1 // no receiver → deadlock
✔ Fix:
go func(){ ch <- 1 }()
<-ch
x Import errors
✔ Fix:
go mod tidy
Advantages of Go
✔ Fast compilation
✔ Simple syntax
✔ Built-in concurrency
✔ No VM required
✔ Cross-platform binaries
✔ Strong typing
x Disadvantages of Go
x No generics (older versions)
x Limited OOP features
x Verbose error handling
x Garbage collector overhead
Fun Facts About Go
- Go was created in 2009
- Designed by engineers at Google
- Compilation is so fast it feels like scripting
- Used in massive systems like Kubernetes
- Has built-in concurrency (rare in languages)
- )
Real-World Use Cases
- Web APIs
- Cloud services
- CLI tools
- Microservices
- Networking tools
Conclusion
Go is one of the best modern programming languages if you want:
- Speed ⚡
- Simplicity 🧠
- Scalability 📈
It is especially powerful for:
Backend + DevOps + Cloud systems
Final Tip
If you’re starting:
- Learn basics (syntax, slices, maps)
- Practice small programs
- Build APIs
- Explore concurrency
0 Comments