Go Introduction
Understanding Go programming language fundamentals
🌟 What is Go?
Go (Golang) is an open-source programming language developed by Google in 2009. It's designed for simplicity, reliability, and efficiency, making it perfect for modern software development and cloud applications.
// Simple Go program structure
package main
import "fmt"
func main() {
fmt.Println("Welcome to Go programming!")
}
Output:
Welcome to Go programming!
Key Go Features
Packages
Organize code into reusable packages
package main
import "fmt"
Functions
First-class functions with multiple returns
func add(a, b int) int {
return a + b
}
Structs
Custom data types and methods
type Person struct {
Name string
Age int
}
Goroutines
Lightweight concurrent programming
go myFunction()
// Runs concurrently
🔹 Go Program Structure
Every Go program follows a basic structure:
// Package declaration (required)
package main
// Import statements
import (
"fmt"
"time"
)
// Main function (entry point)
func main() {
fmt.Println("Current time:", time.Now())
greet("Alice")
}
// Custom function
func greet(name string) {
fmt.Printf("Hello, %s!\n", name)
}
Output:
Current time: 2024-01-15 10:30:45.123456789 +0000 UTC
Hello, Alice!
🔹 Go vs Other Languages
How Go compares to popular programming languages:
Go Advantages:
- Faster than Python: Compiled language with better performance
- Simpler than Java: Less verbose, no complex inheritance
- Safer than C: Garbage collection and memory safety
- More concurrent than JavaScript: Built-in goroutines
// Go: Simple variable declaration
name := "John"
age := 25
// Go: Easy error handling
file, err := os.Open("data.txt")
if err != nil {
log.Fatal(err)
}
🔹 Who Uses Go?
Major companies and projects using Go:
Web Services
Google, Uber, Netflix
Cloud Tools
Docker, Kubernetes
DevOps
HashiCorp, DigitalOcean
FinTech
PayPal, Capital One
🔹 Your First Go Program
Let's create a simple program that demonstrates Go basics:
package main
import "fmt"
func main() {
// Variables
message := "Learning Go is fun!"
count := 42
isActive := true
// Print different types
fmt.Println(message)
fmt.Printf("Count: %d\n", count)
fmt.Printf("Active: %t\n", isActive)
// Call a function
result := multiply(7, 6)
fmt.Printf("7 × 6 = %d\n", result)
}
func multiply(x, y int) int {
return x * y
}
Output:
Learning Go is fun!
Count: 42
Active: true
7 × 6 = 42