CSE224 - Go Concurrent Programming

Introduction

Go Routines

Traditionally, people used OS-level threads for concurrency, but threads are heavyweight. Go introduced goroutines, which are:

  1. Very cheap in terms of memory
  2. Managed by the Go runtime, not the OS
  3. Can be created in large numbers

Channel

A channel is a way for goroutines to communicate and synchronize with each other. It's like a pipe.


  ch := make(chan string)
  go func() {
      ch <- "hello"
  }()

  msg := <-ch
  fmt.Println(msg) // prints: hello
        

Comments

Leave a Comment