Go functions can be written to work on multiple types using type parameters. The type parameters of a function appear between brackets, before the function's arguments.
func Index[T comparable](s []T, x T) int
This declaration means that s is a slice of any type T that fulfills the built-in constraint comparable. x is also a value of the same type.
comparable is a useful constraint that makes it possible to use the == and != operators on values of the type. In this example, we use it to compare a value to all slice elements until a match is found. This Index function works for any type that supports comparison.
- package main
-
- import "fmt"
-
- // Index returns the index of x in s, or -1 if not found
-
- func Index[T comparable](s []T, x T) int {
- for i, v := range s {
- // v and x are type T, which has the comparable
- // constraint, so we can use == here.
- if v == x {
- return i
- }
- }
- return -1
- }
-
- func main() {
- // Index works on a slice of ints
- si := []int{10, 20, 15, -10}
- fmt.Println(Index(si, 15))
-
- // Index also works on a slice of strings
- ss := []string{"foo", "bar", "baz"}
- fmt.Println(Index(ss, "hello"))
- }
A goroutine is a lightweight thread managed by the Go runtime.
go f(x, y, z)
starts a new goroutine running
f(x, y, z)
The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.
Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives. (See the next slide.)
- package main
-
- import (
- "fmt"
- "time"
- )
-
- func say(s string) {
- for i := 0; i < 5; i++ {
- time.Sleep(100 * time.Millisecond)
- fmt.Println(s)
- }
- }
-
- func main() {
- go say("world")
- say("hello")
- }

Channels are a typed conduit through which you can send and receive values with the channel operator, <-.
ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and
// assign value to v.
(The data flows in the direction of the arrow.)
Like maps and slices, channels must be created before use:
ch := make(chan int)
By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.
The example code sums the numbers in a slice, distributing the work between two goroutines. Once both goroutines have completed their computation, it calculates the final result.

Channels can be buffered. Provide the buffer length as the second argument to make to initialize a buffered channel:
ch := make(chan int, 100)
Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.
Modify the example to overfill the buffer and see what happens.
- package main
-
- import "fmt"
-
- func main() {
- ch := make(chan int, 2)
- ch <- 1
- ch <- 2
- fmt.Println(<-ch)
- fmt.Println(<-ch)
- }

A sender can close a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression: after
v, ok := <-ch
ok is false if there are no more values to receive and the channel is closed.
The loop for i := range c receives values from the channel repeatedly until it is closed.
Note: Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.
Another note: Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop.
- package main
-
- import (
- "fmt"
- )
-
- func fibonacci(n int, c chan int) {
- x, y := 0, 1
- for i := 0; i < n; i++ {
- c <- x
- x, y = y, x+y
- }
- close(c)
- }
-
- func main() {
- c := make(chan int, 10)
- go fibonacci(cap(c), c)
- for i := range c {
- fmt.Println(i)
- }
- }

The select statement lets a goroutine wait on multiple communication operations.
A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
- package main
-
- import "fmt"
-
- func fibonacci(c, quit chan int) {
- x, y := 0, 1
- for {
- select {
- case c <- x:
- x, y = y, x+y
- case <-quit:
- fmt.Println("quit")
- return
- }
- }
- }
-
- func main() {
- c := make(chan int)
- quit := make(chan int)
- go func() {
- for i := 0; i < 10; i++ {
- fmt.Println(<-c)
- }
- quit <- 0
- }()
- fibonacci(c, quit)
- }

The default case in a select is run if no other case is ready.
Use a default case to try a send or receive without blocking:
select {
case i := <-c:
// use i
default:
// receiving from c would block
}
- package main
-
- import (
- "fmt"
- "time"
- )
-
- func main() {
- tick := time.Tick(100 * time.Millisecond)
- boom := time.After(500 * time.Millisecond)
- for {
- select {
- case <-tick:
- fmt.Println("tick.")
- case <-boom:
- fmt.Println("BOOM!")
- return
- default:
- fmt.Println(" .")
- time.Sleep(50 * time.Millisecond)
- }
- }
- }
