πŸŽ“ A1Learner
πŸ”
0 of 28 lessons completed

Go Advanced Introduction

Go Advanced is a course about the deeper tools in Go. It builds on generics, channels, and the sync package from earlier courses.

One helper, two types

A generic helper names a placeholder type. The same helper then serves ints and strings.

package main

import "fmt"

func first[T any](items []T) T {
	return items[0]
}

func main() {
	fmt.Println(first([]int{3, 5, 7}))
	fmt.Println(first([]string{"red", "blue"}))
}

A channel hand-off

A goroutine waits for one message on a channel. A WaitGroup holds main until the message lands.

package main

import (
	"fmt"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	jobs := make(chan string, 1)
	wg.Add(1)
	go func() {
		defer wg.Done()
		msg := <-jobs
		fmt.Println("Got:", msg)
	}()
	jobs <- "hello"
	wg.Wait()
	fmt.Println("Done")
}

Example

package main

import (
	"fmt"
	"sync"
)

func first[T any](items []T) T {
	return items[0]
}

func main() {
	fmt.Println(first([]int{9, 4, 2}))
	var wg sync.WaitGroup
	ch := make(chan string, 1)
	wg.Add(1)
	go func() {
		defer wg.Done()
		fmt.Println("Word:", <-ch)
	}()
	ch <- first([]string{"go", "web"})
	wg.Wait()
}

Key points

  • Reflection reads the type of a value while the program runs.
  • The context package carries a cancel signal through a call chain.
  • Concurrency patterns shape how goroutines share and join work.
  • Sync tools guard shared values and run setup a single time.
  • Test files check programs with the go test tool.
  • Generics in depth covers type sets and generic methods.
  • The io package moves bytes through readers and writers.

In this example, the generic helper picks the first int and prints 9. The same helper picks the first string and sends it on the channel. The goroutine prints the word and the WaitGroup lets main finish after it.

Note

The final topics cover unsafe sizes and profiling.

Try it Yourself

Edit the code, then press Run.

Running is simulated Β· Go Playground

Exercise

Which topic reads the type of a value while the program runs?