Dive into Go Programming: A Beginner's Guide

Learn how to set up your Go development environment, write your first program, and understand core concepts like running and building executables. This beginner-friendly guide empowers you to start coding in Go!



Go, a powerful and versatile programming language, is gaining popularity for its simplicity, efficiency, and concurrency features. If you're new to Go, this guide will equip you with the essential knowledge to embark on your programming journey.

Setting Up Your Go Development Environment (IDE)

While you can write Go code in a simple text editor, using an Integrated Development Environment (IDE) offers a more convenient and feature-rich experience. Here's how to set up your IDE:

  1. Choose an IDE: Popular options include Visual Studio Code (VS Code), GoLand (JetBrains), LiteIDE, and Atom. These IDEs provide syntax highlighting, code completion, debugging tools, and Go-specific features.
  2. Install the Go extension: For VS Code users, open the extension manager (Ctrl+Shift+X), search for "Go" by the Go team at Google, and install it. This extension adds Go language support to VS Code.
  3. Verify Go installation: Open a terminal window and type go version. If Go is installed correctly, you'll see the installed version (e.g., go version go1.22.2 windows/amd64).

Go Installation

Relevant installation files are available at https://golang.org/dl/.

Follow the instructions related to your operating system. To check if Go was installed successfully, run the following command in a terminal window:

Syntax
go version
Output
go version go1.21.12 linux/amd64

Installing an IDE for Go

An IDE (Integrated Development Environment) is used to edit AND compile the code.

Popular IDEs include Visual Studio Code (VS Code), Vim, Eclipse, and Notepad. These are all free, and they can be used to both edit and debug Go code.

Note: Web-based IDEs can work as well, but functionality is limited.

We will use VS Code in our tutorial, which we believe is a good place to start.

You can find the latest version of VS Code at https://code.visualstudio.com/.

Go Quickstart

Let's create our first Go program:

  1. Launch the VS Code editor
  2. Open the extension manager or press Ctrl + Shift + X
  3. In the search box, type "go" and hit enter
  4. Find the Go extension by the Go team at Google and install the extension
  5. After the installation is complete, open the command palette by pressing Ctrl + Shift + P
  6. Run the Go: Install/Update Tools command
  7. Select all the provided tools and click OK

VS Code is now configured to use Go.

Open up a terminal window and type:

Syntax
go mod init example.com/hello
Output
go: creating new go.mod: module example.com/hello

Create a new file (File > New File). Copy and paste the following code and save the file as helloworld.go (File > Save As):

Syntax
package main

import ("fmt")

func main() {
fmt.Println("Hello, Go!")
}
Output
Hello, Go!

Now, run the code: Open a terminal in VS Code and type:

Syntax
go run .\helloworld.go
Output
Hello, Go!

Awesome! You have now written and executed your first Go program.

For saving the program as executable, type and run:

Syntax
go build .\helloworld.go
Output
# no output for this command

Alternative for Go Installation

Instead of mentioning GCC (a compiler not typically used for Go), you can highlight the official Go installation instructions:

  • Visit the official download page: https://golang.org/dl/
  • Download the appropriate installer for your operating system.
  • Follow the installation instructions provided on the website.

Integrated Development Environment (IDE) Options

While VS Code is a popular choice, consider mentioning other free and open-source options like:

  • GoLand (JetBrains): Feature-rich IDE with advanced Go support.
  • LiteIDE: Lightweight IDE with Go plugins.
  • Atom: Hackable text editor with Go language support.

Running vs. Building Go Programs

go run: This command directly executes the Go source code without creating a separate executable file. It's convenient for quick testing during development.

go build: This command compiles the Go source code into a platform-specific executable file. This file can be run independently without needing the Go environment.

Saving as Executable

The provided command go build .\helloworld.go is correct for building the executable. There's no output for this command because it silently creates the executable file by default. The filename will be the same as the source file (helloworld.go) without the extension.

Running the Executable

Once built, you can run the executable directly from the terminal: