This course has been updated! We now recommend you take the Basics of Go course.
Table of Contents
Introduction
Introduction
Brenna Martenson gives an overview of the course, and a brief history of the Go language.Installing Go
Brenna describes the steps for installing Go, how to set up directories using the bash profile, and how to run a Go file in the terminal to verify everything is installed correctly.Documentation
Brenna gives an overview of the Golang documentation, and gives suggestions about how to navigate it.Go Documentation Practice
Brenna discusses the first exercise of the course, which is a practice in exploring the Go Documentation.Go vs JavaScript Comparison
Brenna explores the differences between JavaScript and Go in terms of types, structures, conventions, and error handling.Anatomy of a Go File
Brenna describes the structure of a Go file, how to run the file from the terminal, and answers questions about package main and the main() function indispensable in every runnable Go file.
Printing
Printing with fmt
Brenna navigates to the Go playground and explains how to print to standard output.Printing Exercise
Brenna demonstrates how to access the local Go documentation from the terminal, and introduces the exercise on printing.Printing Solution
Brenna live codes the solution for the exercise.
Basic Go Syntax
Types
Brenna introduces Go syntax by giving examples of primitive types, and demonstrates how to verify a type using reflect.TypeOf.Variables
Brenna covers a few examples of variables, describes variable syntax, and explains zero values, which are assigned when a variable is declared without an initial value.Control Structures: If & Else
Brenna demonstrates how blocks are written in Go, how to handle an error returned from a function, and a common way to handle errors using an if block.Control Structures: Switch Statements
Brenna demonstrates how to use switch statements in Go, explains that it is possible to use a conditional statement within switch, and illustrates how to use fallthrough inside of a case.Control Structures: For Loops
Brenna introduces the structure of the for loop, explains its syntax, and demonstrates how to loop over a string using range.Control Structures Exercise
Brenna reviews the control structures discussed in this section, then students are instructed to to iterate over a sentence using range, printing letters with odd indices. The blank identifier is introduced.Control Structures Solution
Brenna live codes the solution to the exercise.
Complex Structures
Functions & Variadic Functions
Brenna demonstrates how to build functions containing for loops, how to name returned variables within the function signature, and how to create variadic functions that take a varying amount of arguments.Functions Exercise
Students are instructed to practice writing functions by creating an average function, and then refactoring the function to be variadic.Functions Solution
Brenna live codes the solution to the exercise.Arrays
Brenna defines an array, explains array syntax in Go, and builds a for loop that iterates over the array.Make
Brenna explains how the built-in method make works, and how it allocates memory.Slices
Brenna explains that a slice is a form of array in Go, demonstrates how to modify its length, and then goes over how to append an element to a slice.Maps
Brenna introduces maps in Go, a data structure which is made up of key value pairs.Complex Structures Exercise
Students are instructed to practice working with arrays, slices and maps by completing the different parts of the complex structures exercise.Complex Structures Solution
Brenna live codes the solution to the exercise.
Go Toolkit
Tools & Commands
Brenna introduces common Go terminal commands, such as "go run" and "go install", and demonstrates how they might be used in a Go project.Packages
Brenna introduces the required package name at the top of every Go file, exported and unexported functions, the linting rules for exported functions, and explains how to run exported functions in other Go packages.Testing
Brenna introduces the testing package that is built into the Go standard library, then gives an example of a test function and discusses the naming conventions of a test file.Unit Testing Exercise
Students are instructed to write a new unit test to ensure a preexisting function runs as expected, and then to use test-driven development (TDD) to write code to make a preexisting test pass.Unit Testing Solution
Brenna live codes the solution to the exercise.
Structs
Structs
Brenna introduces structs in Go and demonstrates how to define a type on a struct, how to instantiate a struct using a struct literal, and how to access fields on a struct with dot notation.Custom Types
Brenna demonstrates how to nest structs inside of other structs, and how to create custom types.Accessing Nested Structs
Brenna codes an example of how to instantiate a nested struct using a struct literal, then demonstrates how to access the values through the parent struct.Modifying Struct Values Exercise
Students are instructed to modify the existing code to change an existing field on a struct if a condition is met. Brenna gives helpful hints, and also asks students to draw conclusions on why a phenomenon is happening in the code.Modifying Struct Values Solution
Brenna live codes the solution to the struct exercise, and foreshadows the coming chapter on pointers and references.
Pointers
Pointers
Brenna introduces pointers by demonstrating how they allow explicit pass by reference as opposed to pass by value. An example is given on how to to point to a specific memory address of a value, and how to dereference the address to get the value stored at the memory address.Pass by Reference
Brenna live codes an example that highlights the difference between passing by value and passing by reference, and demonstrates how struct values can be modified.Pointers Exercise
Students are instructed to write a function that takes in a pointer variable and uses it to modify the value the pointer is pointing to.Pointers Solution
Brenna live codes the solution to the exercise.
Error Handling
Error Handling
Brenna gives an overview of the different types of error handling in Go, namely error and panic.Panic, Defer & Recover Practice
Brenna goes over a practice of the different error handling methods in Go: what a function should defer to when there is an error, when a function should panic, and how a function can recover from a panic.
Methods
Methods
Brenna introduces methods in Go by distinguishing them from functions.Methods Exercise
Students are instructed to refactor functions from earlier in the course to be methods, and modify the main function to accept these methods.Methods Solution
Brenna live codes the solution to the exercise.
Interfaces
Interfaces
Brenna defines what an interface is, namely a list of methods that describes the behavior of types that implement it.Refactor for an Interface
Brenna live codes a describer interface, and adds a method that takes in any type that satisfies the describer interface.Empty Interfaces
Brenna defines what an empty interface is, what type of values it can hold, and explains how the empty interface can be useful.
Web Servers
Web Servers & Routes
Brenna begins building a todo app using Go. Through a code-along exercise, students are shown how to spin up a basic web server and handle routes.Go Templates
Brenna creates another endpoint for the todo application, and uses Go templates to generate HTML output on a webpage. How to send the appropriate server error to the webpage when a template cannot be parsed is also covered.Template Errors & Data
Brenna gives a brief introduction to what the go template is doing, then demonstrates what an error would look like if the template cannot be parsed by the application. How to pass data into the template to be rendered when the template is executed is then live-coded into the application.Submitting a Form
Brenna uses the http package to parse the data submitted to the todo application form, and summarizes the goals as well as the limitations of the application.
Hitting an External API
External APIs
Brenna introduces the section on getting data from an external API by visiting the website for the Star Wars API, the API that will be used for the rest of this section. After hitting one of the API endpoints on the site, students are instructed to create a basic route.Making an HTTP Request
Brenna demonstrates how to access the Star Wars API by live coding a function to make a GET request to the API endpoint.Parsing an HTTP Response
Brenna demonstrates how to use json.Unmarshal to parse JSON-encoded data from an HTTP response and store the received data into Go structs.Getting Nested JSON Data
Brenna uses the data from the HTTP response to make additional GET requests to the API, gathering information about planets for each returned person.