Introduction to Go
Learn Go through interactive, bite-sized lessons. Build scalable applications with modern concurrency.
Start Go Journey →Go, also known as Golang, is a statically typed, compiled programming language designed by Google. It combines the efficiency of compiled languages with the ease of use of dynamic languages.
Key Features of Go
- Simplicity and readability
- Fast compilation and execution
- Built-in concurrency support
- Garbage collection
- Strong standard library
Getting Started with Go
To begin your Go journey, you'll need to install Go on your system. Once installed, you can create your first Go program.
Your First Go Program
Here's a simple "Hello, World!" program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Let's break down this program:
package main: Declares the package name. Themainpackage is special in Go, as it defines an executable program.import "fmt": Imports thefmtpackage, which provides formatting and printing functions.func main(): Defines the main function, the entry point of the program.fmt.Println("Hello, World!"): Prints the string to the console.
Basic Syntax and Concepts
Go's syntax is clean and straightforward. Here are some fundamental concepts:
Variables
Go supports type inference, allowing you to declare variables without explicitly specifying their type:
var x int = 5
y := 10 // Short variable declaration
Functions
Functions in Go can return multiple values, making error handling more convenient:
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
Control Structures
Go provides familiar control structures like if-else statements and for loops:
if x > 0 {
fmt.Println("Positive")
} else {
fmt.Println("Non-positive")
}
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Concurrency in Go
One of Go's standout features is its built-in support for concurrency through goroutines and channels.
Goroutines
Goroutines are lightweight threads managed by the Go runtime:
go func() {
// This function runs concurrently
fmt.Println("Hello from a goroutine!")
}()
Channels
Channels facilitate communication between goroutines:
ch := make(chan int)
go func() {
ch <- 42 // Send value to channel
}()
value := <-ch // Receive value from channel
Next Steps
To deepen your understanding of Go, explore these topics:
Go's simplicity, efficiency, and powerful features make it an excellent choice for modern software development. Start coding and experience the Go way of programming!