Introduction

Go’s concurrency model is one of its core features. This article covers the basics of goroutines and channels.

Goroutine Basics

package main

import (
    "fmt"
    "time"
)

func sayHello() {
    fmt.Println("Hello from goroutine!")
}

func main() {
    go sayHello() // Launch a goroutine

    time.Sleep(time.Second) // Wait for goroutine to execute
    fmt.Println("Main function")
}

Channel Communication

package main

import "fmt"

func main() {
    ch := make(chan string)

    go func() {
        ch <- "Message from goroutine"
    }()

    msg := <-ch
    fmt.Println(msg)
}

Summary

Go’s concurrency model is simple yet powerful. With goroutines and channels, you can easily build highly concurrent programs.