
Basics of Go
Learning Paths:
Topics:
Table of Contents
Introduction
What is Go
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
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
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
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
Crytpomasters Setup
Maximiliano walks through setting up the initial packages for a console application that utilizes the cex.go package to make API requests and retrieve data.Making an HTTP Request
Maximiliano demonstrates the usage of the strings package to perform basic operations on UTF-8 encoded strings, such as manipulation and transformation. It's important to note that these functions generate a new copy of the string and do not modify the original.Response & Error Handling
Maximiliano showcases the creation of response and error handlers using the built-in HTTP status codes provided by the Go http package. These handlers are useful for managing and handling various HTTP responses and errors in a Go application.Printing The API Response
Maximiliano demonstrates how to make an API call from the main package and showcases the format of the returned data.JSON Parsing with Unmarshal
Maximiliano demonstrates the process of parsing and storing JSON data using the Unmarshal function from the json package. The Unmarshal function decodes the JSON-encoded data and saves the result in the provided value, which should be a pointer.Mapping Between JSON and Go Struct
Maximiliano creates a Go struct that aligns with the API's JSON response structure, using a JSON-to-Go converter tool. It emphasizes that if the property types in the struct do not match the JSON data, the Unmarshal function will raise an error.WaitGroups
Maximiliano illustrates that the order of API data returned from a Get call is a synchronous operation. Additionally, the segment discusses utilizing waiting groups to wait for a collection of goroutines to finish.Async API Calls with GoRoutines
Maximiliano demonstrates the creation of a lambda function that functions as a GoRoutine, allowing for the containment and synchronous execution of API calls. This approach enables efficient handling and coordination of API requests within the Go program.Testing
Maximiliano explores Go's built-in testing capabilities, including defining test suites, testing functions, and Go fuzzing. Fuzzing, a form of automated testing that systematically manipulates program inputs to uncover bugs, is also covered in this segment.
Creating a Server
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.