Basics of Go

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
7 hours, 57 minutes CC
Basics of Go

Course Description

Get a quick introduction to Go. Learn basic syntax like variables, constants, data types, and creating and importing packages. You’ll get hands-on with functions, control structures, error handling, type definitions, structures, and interfaces. Create a webserver which handles API requests and uses Go templates. Finally, see a few advanced topics like goroutines, channels, and async API calls.

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: August 8, 2023

Topics

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Introduction

Section Duration: 3 minutes
  • Introduction
    Maximiliano Firtman introduces the course by sharing some personal and professional background. The overview includes key topics covered, such as Go fundamentals, standard libraries, goroutines, the fmt package, the http package, templating, and web servers, providing a clear outline of the course content.

What is Go

Section Duration: 52 minutes
  • The History of Go
    Maximiliano provides a brief overview and history of Go including its connection to Golang. The discussion highlights the three main goals of the Go language: ease of coding, efficient compilation, and efficient execution.
  • Characteristics & Timeline
    Maximiliano explores the key characteristics of Go, such as its static type system, garbage collection, and single binary compilation. The discussion also includes an overview of the timeline of Go, starting from its inception in 2007 to the release of Go 1.20 in 2023, providing a glimpse into the evolution of the language over time.
  • Go.dev Overview
    Maximiliano guides students through the official Go website and explains the organization of its documentation. The discussion includes a brief demonstration of the Tour of Go tutorial and an overview of the learning resources available.
  • Philosophy & Tools
    Maximiliano explores companies that use Go and the main values of the language. The discussion also covers the capabilities of the Go CLI and the tooling required for developing Go applications.
  • Go Setup
    Maximiliano walks through selecting the appropriate Go extension for Visual Studio Code, checking Go installation, and creating a basic stand-alone Go script. The discussion also covers how to run the Go script from the terminal.
  • Multi Platform & Common Use Cases
    Maximiliano explores the multi-platform capabilities of Go, highlighting its ability to generate executable binary files for various platforms and operating systems. The discussion further covers common use cases for Go, such as web services, DevOps, desktop UI, and machine learning.

Variables, Constants, and Data Types

Section Duration: 1 hour, 9 minutes
  • Go Basics Overview
    Maximiliano discusses fundamental syntax rules of Go, emphasizing the use of curly braces to contain code blocks, the case-sensitivity of the language, and its non-object-oriented nature without classes. The discussion further covers essential rules governing the contents of folders and files in Go.
  • Create a Go Module
    Maximiliano showcases the process of creating and running a module using the Go command-line interface (CLI). The discussion also covers the topic of creating multi-module applications using workspaces.
  • Variables, Types & Const
    Maximiliano explains the syntax for defining variables, constants, and data types in Go. The discussion also highlights Go's capability to infer data types and introduces a shortcut for defining variables.
  • Data Types
    Maximiliano demonstrates Go's pre-defined data types, including strings, booleans, and numeric types like integers and floating-point values. The discussion also addresses student questions about defining constants globally and Go's ability to automatically determine a variable's data type.
  • Packages
    Maximiliano covers the topic of defining and importing packages in Go, explaining the scope of functions within these packages. The discussion includes a demonstration using dot syntax to access data from imported packages and how to define public or private visibility.
  • Packages Q&A
    Maximiliano demonstrates helpful functionality in VS Code for importing packages and performing linting in Go development. The discussion also covers a student's question about exporting data from packages other than the main package.
  • Visibility
    Maximiliano gives a concise overview of visibility in Go, explaining the concepts of defining public or private elements and discussing the various scoping options for variables and lambda functions.
  • Numbers & Collections
    Maximiliano explores the usage of global functions for converting between different number types in Go, and introduces collections such as arrays, slices, and maps. It is important to note that collections in Go are not objects and can be manipulated using global functions.

Functions, Control Structures, & Error Handling

Section Duration: 1 hour, 22 minutes
  • Functions
    Maximiliano highlights the characteristics of functions in Go, emphasizing their capability to return multiple values and use labeled variables. The segment includes a demonstration of how to handle functions that return multiple variables.
  • Pointers & References
    Maximiliano showcases the usage of pointers in Go, which allows creating references to the original value of a passed variable. It's important to note that unless a reference is used, Go automatically creates a copy of the passed variable.
  • panic, defer, & Error Design Pattern
    Maximiliano demonstrates Go's less common control flow mechanisms, panic and defer, along with design patterns for error handling. These mechanisms involve triggering runtime errors, halting program execution, and deferring function evaluations.
  • Control Structures
    Maximiliano explores Go's commonly used control structures, including if, for, and switch, highlighting the differences in formatting compared to JavaScript. The switch structure is particularly useful as it can include conditions and serve as a replacement for extensive if statements.
  • Calculator Project
    Maximiliano guides you through the process of creating a basic calculator in Go, which takes input from the standard input using Scanf. Additionally, different verb types for the fmt package functions are explained in this segment.
  • Reading Files
    Maximiliano showcases the usage of os.ReadFile to read the contents of a text file, and provides guidance on handling errors that may occur during a failed read attempt.
  • Writing Files
    Maximiliano illustrates the process of writing a file using os.WriteFile and creating more intricate messages with fmt.Sprintf. The segment also addresses a student's question about whether package names and directories must match.

Type Definitions, Structures, & Interfaces

Section Duration: 1 hour, 22 minutes
  • Type Definitions
    Maximiliano showcases the declaration of new types, the addition of methods to types, and referencing types using aliases. However, it is important to note that methods cannot be attached to type aliases.
  • Type Definitions Q&A
    Maximiliano addresses a student's question about defining methods with string types and provides an additional example that demonstrates defining types with an attached method to clarify the purpose of the attribute arguments.
  • Structs
    Maximiliano explores structures, which are data types that have strongly typed properties, default constructors, and the ability to include methods.
  • Struct Methods
    Maximiliano demonstrates creating a data structure and explains how to import and access the structure data in a different package. It is important to make the properties of the data structure public in order to access them.
  • Factories
    Maximiliano showcases patterns for creating factory functions that generate instances of structs or interfaces. These factory functions encapsulate the creation logic and offer a convenient way to construct instances of complex or customizable types.
  • Implementing the Stringer Interface
    Maximiliano demonstrates implementing a method that modifies the default output of a type to a customized string format.
  • Embedding
    Maximiliano showcases type embedding, allowing the parent type to inherit the properties of the embedded type. The segment also covers accessing the embedded data in a factory function.
  • Interfaces
    Maximiliano demonstrates how to create interfaces in Go, which define a set of methods that can be used as a type. Interfaces in Go have implicit implementation and can emulate polymorphism commonly found in object-oriented programming.
  • Goroutines
    Maximiliano discusses goroutines, which are Go's way of utilizing threads, and demonstrates how to open a goroutine using the "go" prefix. This segment also covers delaying thread execution using time.Sleep.
  • Channels
    Maximiliano demonstrates creating channels for inter-goroutine communication and addresses a student's question about passing channels between multiple packages. Buffer channels are also introduced to control the number of values that can be sent at once.

Creating an API Client

Section Duration: 1 hour, 32 minutes

Creating a Server

Section Duration: 1 hour, 31 minutes
  • Building a Local HTTP Server
    Maximiliano provides an overview of options for utilizing Go in web development, such as WebAssembly, transpiling to JavaScript, web servers, and web services. Additionally, a step-by-step guide on setting up a basic local HTTP server is included in this segment.
  • Logging & Watching for Changes
    Maximiliano addresses a student's question about enabling debugging and demonstrates the process of moving the server function outside of the main function. The segment also mentions that Go does not provide a built-in tool for automatically restarting the server when a file changes, requiring the use of a third-party tool.
  • Serving Files
    Maximiliano demonstrates the usage of the FileServer utility function to create a file server that automatically searches the specified directory for files to be served.
  • Go Templates
    Maximiliano explores the usage of Go templates for server-side rendering, emphasizing their ability to generate dynamic content like HTML or configuration files. By separating the presentation logic from the data, Go templates facilitate easier management and updates.
  • Serving from Data Structures
    Maximiliano responds to student inquiries about the required imports and clarifies whether the dot in the template represents a type. Additionally, the segment includes a demonstration of serving a museum exhibition from a data structure.
  • Template Looping & Conditional HTML
    Maximiliano showcases the usage of the range keyword to iterate over the existing data structure and dynamically render the remaining museum exhibitions. The segment also covers the conditional rendering of HTML using if-else statements.
  • API Handlers for GET
    Maximiliano illustrates the process of creating handlers to make API GET requests and retrieve all the requested items. The segment also covers the handling of request errors and demonstrates how to request data by ID.
  • API Handlers for POST
    Maximiliano demonstrates the process of creating a handler for handling API POST requests. The segment covers the handling of POST errors and includes a practical demonstration of a successful POST request.
  • Frameworks & External Libraries
    Maximiliano discusses the benefits of using frameworks and external libraries to simplify the process of building applications in Go. The segment includes the implementation of the Gin web framework as an example to demonstrate how frameworks or external libraries can be utilized in Go development.
  • Compiling & Packaging
    Maximiliano covers the process of compiling a Go project, including the flexibility to compile for various platforms and operating systems. Additionally, in order to embed an application's assets, a package needs to be created, as Go produces binary files through compilation.

Wrapping Up

Section Duration: 2 minutes

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now