Advanced Go in Go

Advanced Go covers two tools that real Go programs use every day.

In this chapter, you will:

  • inspect types and values while the program runs, with reflection
  • cancel work and set time limits, with context

What each lesson covers:

  • Reflection: Go reflection with the reflect package: reflect.Type, reflect.Value and Kind, inspecting types and struct fields and tags, reading and setting values, and DeepEqual.
  • Context: Go's context package: context.Context and Background, cancellation, deadlines and timeouts, context values, and using context in HTTP requests, goroutines and database calls.

This chapter has 2 lessons. Plan on about 50 minutes, with 6 exercises to check what you learned. Every example is real Go code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.

Simple lesson: finish this chapter and you can read and write the code found in real Go servers.

Lessons in this section

  1. Reflection

    Go reflection with the reflect package: reflect.Type, reflect.Value and Kind, inspecting types and struct fields and tags, reading and setting values, and DeepEqual.

    • Explain what reflection is and when to use it
    • Use reflect.Type, reflect.Value and Kind
    • Inspect struct fields and tags
    • Read and change values through reflection
  2. Context

    Go's context package: context.Context and Background, cancellation, deadlines and timeouts, context values, and using context in HTTP requests, goroutines and database calls.

    • Explain what context is and why it is used
    • Use context.Background and context.Context
    • Cancel work, and set deadlines and timeouts
    • Carry request values safely