forked from microsoft/aed-go-learn-content
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version of the "Getting started with Go" module
- Loading branch information
Showing
10 changed files
with
430 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
# Introduction | ||
|
||
< Introductory Content > | ||
In this module, you’ll start taking your first steps with Go. First, you’ll begin by learning a little bit about Go’s history and the main features of this programming language. Then, you’ll be installing and configuring Go on your workstation. Additionally, you’ll explore another way you can play with Go without having to install anything on your workstation. You’ll also learn how Go requires you to organize your code so that you don’t have any problems building them when you need to develop complex applications. | ||
|
||
At the end of the module, you’ll create your first “Hello World!” application in Go, and you’ll be ready to continue learning more about how to build applications using Go. | ||
|
||
## Learning Objectives | ||
|
||
In this module, you will begin to discover: | ||
In this module, you will: | ||
|
||
- | ||
* Install and configure Go in your local workstation | ||
* Install and configure VS Code and the Go extension | ||
* Explore the Go Playground | ||
* Create your first Go application | ||
|
||
## Prerequisites | ||
|
||
- | ||
## Prerequisites | ||
* Programming basics in general | ||
* Knowledge with at least one programming language | ||
* Download / Install Software from the Internet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
# What is Go | ||
Go is a programming language developed at Google, announced in 2009 as an open-source project by Robert Griesemer, Rob Pike, and Ken Thompson. Since then, Go has been used for developing other very known technologies like Docker, Kubernetes, Terraform, and others. But Go, it’s a general-purpose language too. The goals of the language were to be expressive, efficient, and effective when writing reliable and robust code. | ||
|
||
< Unit Text > | ||
According to the [TIOBE index](https://www.tiobe.com/tiobe-index/), in 2009 and [2016 Go was the programming language of the year](https://insights.dice.com/2017/01/10/go-tiobe-programming-language-2016/). And although it reached its tipping point that year, Go has [maintained a sustained rating over the years](https://www.tiobe.com/tiobe-index/go/). | ||
|
||
Go has many similarities with C inheriting many aspects of the C’s syntax, control-flow statements, basic data types, pointers, and others. However, Go it’s more than an up to date version of C. It borrows and adapts ideas from other programming languages, and removes any unnecessary features that could bring complexity to the language. For instance, some portions of the object oriented programming paradigm are not implemented as fully in Go, and you’ll learn why in some of the upcoming modules. | ||
|
||
Go is very idiomatic in very different topics like source code formats, not allowing unused code, not requiring an IDE, preferring to use standard libraries than frameworks, and offering a different approach for concurrent programming and error handling. | ||
|
||
Go runs on Unix systems like Linux and macOS, and Windows is supported as well. | ||
|
||
## Go Principles | ||
|
||
To understand why some things in Go are they way they are, it’s worth to understand what are the principles behind this programming language: | ||
|
||
* Go strives to keep things small and simple, or do more in only a few lines of code | ||
* Concurrency is a first citizen, functions can run as lightweight threads | ||
* Compilation and execution is fast, the aim is to be as fast as C | ||
* Requires casting to be explicit, otherwise it throws a compilation error | ||
* Unused code is not a warning, but an error (won’t compile) | ||
* There’s an official formatting that helps to maintain consistency across projects | ||
* Go is not a good friend of frameworks as it prefers the usage of standard libraries | ||
* Guarantee backwards compatibility | ||
* Go’s license is completely open source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,187 @@ | ||
# Exercise - Install Go | ||
Before you start creating applications with Go, you need to set up your development environment. | ||
|
||
< Unit Text > | ||
In case you don’t want to install Go locally, you can use the [Go Playground](https://play.golang.org/) which is a web service that can run applications written in Go in a browser. This is a great option when you want to quickly and easily run code examples. However, we do recommend setting up your local environment when building applications that require a more complex code organization. | ||
|
||
|
||
::: zone pivot="macos" | ||
## Install Go on macOS | ||
To install Go on macOS, you can do it using Homebrew, or download the Go installer from the downloads page at [https://golang.org/dl/](https://golang.org/dl/). Here you can find both approaches, choose only one. | ||
|
||
### Install Go using Homebrew | ||
The simplest way to install Go is using Homebrew. | ||
|
||
Open a terminal prompt, and run the following commands to install the latest version of Go: | ||
|
||
``` | ||
brew update | ||
brew install go | ||
``` | ||
|
||
Homebrew installs Go at `/usr/local/go`, and the path `/usr/local/go/bin` should now be part of the **$PATH** environment variable. You might need to restart your terminal prompt to confirm that Go is installed correctly. | ||
|
||
To confirm that Go is installed by running the following command: | ||
|
||
``` | ||
go version | ||
``` | ||
|
||
### Install Go using the Go Installer | ||
|
||
Alternatively, you can follow these steps to install the latest version of Go. | ||
|
||
**Step 1: Download the Go installer** | ||
|
||
Head over to the [Go download page](https://golang.org/dl/). In the “Feature downloads” section, click on the “Apple macOS” option to download the Go installer. | ||
|
||
You might see a window prompting you to allow downloading files from golang.org; select “Allow” to start downloading the Go installer. | ||
|
||
**Step 2: Run the Go installer** | ||
|
||
Once you have the Go installer locally, you can start with the installation. Double click on the .pkg file, and follow the instructions to install Go. | ||
|
||
By default, the .pgk file installs go at `/usr/local/go`, and the path `/usr/local/go/bin` should now be part of the **$PATH** environment variable. | ||
|
||
**Step 3: Confirm that Go is installed correctly** | ||
|
||
Once the installation finishes, open a new terminal prompt and run the following command: | ||
|
||
``` | ||
go version | ||
``` | ||
|
||
You should see the details of the Go version installed on your workstation. | ||
|
||
::: zone-end | ||
|
||
::: zone pivot="linux" | ||
## Install Go on Linux | ||
|
||
To install Go on Linux, you need to download the Go installer from the downloads page at [https://golang.org/dl/](https://golang.org/dl/). If, for some reason, you have Go already installed and want to install the latest version, remove the existing installation before proceeding. | ||
|
||
**Step 1: Download the Go installer** | ||
|
||
Head over to the [Go download page](https://golang.org/dl/). In the “Feature downloads” section, click on the “Linux” option to download the Go installer. You might see a window prompting you to allow downloading files from golang.org; select “Allow” to start downloading the Go installer. | ||
|
||
Alternatively, you can download the installer using the following command (you might need to change the version number if 1.15.4 it’s not the [latest version](https://golang.org/doc/devel/release.html) at the time you’re following this guide): | ||
|
||
|
||
``` | ||
wget https://golang.org/dl/go1.15.4.linux-amd64.tar.gz | ||
``` | ||
|
||
**Step 2: Extract the Go installer** | ||
Once you have the Go installer locally, you can start setting up Go in your workstation. | ||
|
||
Extract the installer at `/usr/local/go `and run the following command as root or through sudo: | ||
|
||
``` | ||
tar -C /usr/local -xzf go1.15.4.linux-amd64.tar.gz | ||
``` | ||
|
||
You need to add the path `/usr/local/go/bin` to the **$PATH** environment variable. You can either add the following command to your $HOME/.profile or /etc/profile (so Go is system-wide available): | ||
|
||
``` | ||
export PATH=$PATH:/usr/local/go/bin | ||
``` | ||
|
||
You need to log out and log in again to update the **$PATH** environment variable. Alternatively, you can run the following command to force the update: | ||
|
||
``` | ||
source $HOME/.profile | ||
``` | ||
|
||
**Step 3: Confirm that Go is installed correctly** | ||
Once you have the Go distribution configured, confirm that Go works by running the following command: | ||
|
||
``` | ||
go version | ||
``` | ||
|
||
You should see the details of the Go version installed on your workstation. | ||
|
||
::: zone-end | ||
|
||
::: zone pivot="windows" | ||
## Install Go on Windows | ||
To install Go on Windows, you need to download the Go installer from the downloads page at [https://golang.org/dl/](https://golang.org/dl/). | ||
|
||
**Step 1: Download the Go installer** | ||
|
||
Head over to the [Go download page](https://golang.org/dl/). In the “Feature downloads” section, click on the “Microsoft Windows” option to download the Go installer. | ||
|
||
You might see a window prompting you to allow downloading files from golang.org; select “Allow” to start downloading the Go installer. | ||
|
||
**Step 2: Run the MSI Go installer** | ||
|
||
Once you have the Go installer locally, you can start with the installation. Double click on the .msi file, and follow the instructions to install Go. | ||
|
||
By default, the .pgk file installs go at `C:\Go`, and the path `C:\Go\bin` should now be part of the **$PATH** environment variable. | ||
|
||
**Step 3: Confirm that Go is installed correctly** | ||
Once you have the Go distribution configured, confirm that Go works. | ||
|
||
Open a new command prompt window (cmd) or Powershell, and run the following command: | ||
|
||
``` | ||
go version | ||
``` | ||
|
||
You should see the details of the Go version installed on your workstation. | ||
|
||
::: zone-end | ||
|
||
## How To Organize Your Code Projects | ||
Please read this section carefully before you continue. | ||
|
||
Go differs from other programming languages regarding how to organize project files. First, Go works under the concept of workspaces, which is nothing but a location where your application source code lives. In Go, all projects share the same workspace. However, since version 1.11 Go started to change this approach. You don’t have to worry about that yet, we’ll cover this part in the next module. So, typically, the Go’s workspace is located at $HOME/go, but you can set up a different location for all your projects if needed. | ||
|
||
To define a different workspace location, set a value to the **$GOPATH** environment variable. Make sure you have a value for this environment variable to avoid having problems in the future when you create complex projects. | ||
|
||
In macOS or Linux, you can configure your workspace by adding the following command to your `~/.profile`: | ||
|
||
``` | ||
export GOPATH=$HOME/go | ||
``` | ||
|
||
Then run the following command to update your environment variables: | ||
|
||
``` | ||
source ~/.profile | ||
``` | ||
|
||
In Windows, create a folder like `C:\Projects\Go `where you’ll create all Go projects`.` Open a PowerShell prompt, and run the following command: | ||
|
||
``` | ||
[Environment]::SetEnvironmentVariable("GOPATH", "C:\Projects\Go", "User") | ||
``` | ||
|
||
You can get the workspace’s location in Go by printing the value of the **$GOPATH** environment variable for future references. Or, getting the environment variables that matters to Go running the following command | ||
|
||
``` | ||
go env | ||
``` | ||
|
||
In your Go’s workspace, you’ll find the following folders | ||
|
||
* **bin** which contains executables from applications | ||
* **src** which includes all applications source code that lives in your workstation | ||
* **pkg** which contains compiled versions of the available libraries; the compiler can link against these libraries without recompiling them | ||
|
||
For instance, this is how it can look like the folder structure tree in a workstation: | ||
|
||
``` | ||
bin/ | ||
hello | ||
coolapp | ||
pkg/ | ||
github.com/gorilla/ | ||
mux.a | ||
src/ | ||
github.com/golang/example/ | ||
.git/ | ||
hello/ | ||
hello.go | ||
``` | ||
|
||
We’ll go back to talk about this topic in the next module, and what you need to do if you need or want to have your project outside of the **$GOPATH**. Additionally, you can go deeper into this topic by [visiting the official documentation site](https://golang.org/doc/gopath_code.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
# Exercise - Set up your development environment | ||
We mentioned early in this module that if you don’t want to install Go on your local workstation yet, you can use the [Go Playground](https://play.golang.org/), a website that compiles and runs Go code inside a sandbox environment. Typically, this is the tool you use for sharing code snippets with other developers when sharing a solution in forums. | ||
|
||
< Unit Text > | ||
However, the Go Playground comes with a few limitations that might impact your learning journey, like: | ||
|
||
* Keyboard inputs are not supported | ||
* Random numbers are deterministic | ||
* Time is constant. You get the same value every time. | ||
|
||
 | ||
|
||
Head over to the Go Playground, and click on the “Run” button. | ||
|
||
Feel free to continue using this site for learning purposes, but we highly recommend installing Go locally and using an IDE to improve your development productivity. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,73 @@ | ||
# Exercise - Set up your development environment | ||
You can use any text editor to write Go applications, but when you use an IDE like Visual Studio Code (VS Code), you write code faster. Without an IDE, you’d have to write every line from scratch, which at the beginning could help to get familiar with the syntax, but in the long-term, you might need more time to write an application. Therefore, we recommend using an IDE. For instance, you could start using VS Code, which is free and open-source, which has an extension for Go to help you write code faster. | ||
|
||
When you have VS Code and the Go extension, you get the following benefits: | ||
|
||
* Format applications following Go standards | ||
* Add package references as soon as you add a new line and save your code. | ||
* Remove package references that your application is not using anymore. | ||
* Debug your applications by allowing you to set breakpoints and use watchers at runtime | ||
* Provides suggested completions for code and package references | ||
* Hover information to get more details about the code referencing its documentation | ||
* Provides signature help like listing the parameters a function has | ||
* Navigate the project’s code easily when you have multiple files | ||
* Support for testing like generating test skeletons for functions | ||
|
||
Below are the instructions to install Visual Studio Code on your workstation. | ||
|
||
## Install Visual Studio Code on macOS | ||
|
||
**Step 1: Download Visual Studio Code** | ||
Head over to the Visual Studio Code page and click on the “Mac” box to download the app. Wait a few seconds. You’ll see a prompt to save the file locally. | ||
|
||
**Step 2: Move the app to the Applications folder** | ||
Locate the file you’ve just downloaded and open Finder. Drag and drop the Visual Studio Code file to the Applications folder in Finder. | ||
|
||
**Step 3: Start the app from the Applications folder** | ||
To start the Visual Studio Code app, double-click on the icon from the Applications folder. | ||
|
||
**Important!** | ||
You might see a warning that Visual Studio Code can't be opened because Apple can't check it for malicious software. If that happens, select OK to dismiss the message. Then right-click Visual Studio Code in the Applications folder and choose the Open menu. If you choose this option, Visual Studio Code should open without any further issues. | ||
|
||
## Install Visual Studio Code on Linux | ||
|
||
**Step 1: Download the Visual Studio Code installer** | ||
Head over to the Visual Studio Code page and click on the “.deb” or “.rpm” option (depending on if your Linux distribution uses deb-based or rpm-based package managers) from the “Linux” box to download the app. Wait a few seconds. You’ll see a prompt to save the file locally. | ||
|
||
**Step 2: Start the Visual Studio Code installer** | ||
Locate the file you’ve just downloaded, and open it by double-clicking the file. Follow the instructions to install the app on your workstation. | ||
|
||
**Step 3: Start Visual Studio Code** | ||
When the installation process is complete, open Visual Studio Code to confirm that it’s working. | ||
|
||
--------------- | ||
|
||
Alternatively, you can install Visual Studio Code [using a package manager like APT or YUM](https://code.visualstudio.com/docs/setup/linux). | ||
|
||
## Install Visual Studio Code on Windows | ||
|
||
**Step 1: Download the Visual Studio Code installer** | ||
Head over to the Visual Studio Code page and click on the “Windows” box to download the app. Wait a few seconds. You’ll see a prompt to save the file locally. | ||
|
||
**Step 2: Start the Visual Studio Code installer** | ||
Locate the file you’ve just downloaded, and open it by double-clicking the file. Follow the instructions to install the app on your workstation. | ||
|
||
**Step 3: Start Visual Studio Code** | ||
When the installation process is complete, open Visual Studio Code to confirm that it’s working. | ||
|
||
## Install the Go extension for Visual Studio Code | ||
The Go extension for VS Code will help you write Go apps more efficiently because you’ll enjoy all of the features we mentioned early regarding why it’s a good idea to use an IDE for developing Go apps. | ||
|
||
Open VS Code, in case you haven’t done it already. | ||
|
||
**Step 1: Open the Extensions view** | ||
In VS Code, go to “View” and select “Extensions” from the menu bar, or select the “Extensions” icon from the left side of the screen. | ||
|
||
**Step 2: Search for the Go extension** | ||
Enter “Go” in the search box at the top of the Extensions view. | ||
|
||
Select the extension published by the “Go Team at Google” (usually the first one in the list). You’ll see the details of the extension on the right side of the screen. | ||
|
||
**Step 3: Install the Go extensions** | ||
Below the extension’s description, you’ll see an **Install** button; click on it to install the extension in your workstation. When the installation is complete, the button’s text will change to **Uninstall**, meaning that you’ve successfully installed the Go extension. | ||
|
||
< Unit Text > |
Oops, something went wrong.