Hello!

This post is the start of a series of posts about golang. In this one, we will install golang and configure vscode.

Install go

The current version of golang can be downloaded from official site. I’m using Linux so I’ll download the Linux version

$ wget https://go.dev/dl/go1.19.2.linux-amd64.tar.gz
--2022-10-28 22:35:40--  https://go.dev/dl/go1.19.2.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 216.239.36.21, 216.239.34.21, 216.239.38.21, ...
Connecting to go.dev (go.dev)|216.239.36.21|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.19.2.linux-amd64.tar.gz [following]
--2022-10-28 22:35:40--  https://dl.google.com/go/go1.19.2.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 216.58.215.78, 2a00:1450:401b:810::200e
Connecting to dl.google.com (dl.google.com)|216.58.215.78|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 148883574 (142M) [application/x-gzip]
Saving to: ‘go1.19.2.linux-amd64.tar.gz’

go1.19.2.linux-amd64.tar.gz         100%[================================================================>] 141,99M  20,8MB/s    in 13s

2022-10-28 16:36:03 (10,6 MB/s) - ‘go1.19.2.linux-amd64.tar.gz’ saved [148883574/148883574]

Now you need to unpack the archive to /usr/local folder

sudo tar -C /usr/local -xzf go1.19.2.linux-amd64.tar.gz

For the go command to become available, it must be added to the PATH. I use zsh, so I will add the path to go to the ~/.zshrc file

export PATH=$PATH:/usr/local/go/bin

After restarting the terminal, the go command is available

$ go version
go version go1.19.2 linux/amd64

Vscode install

To make it convenient to write go code, I use the Visual Studio Code text, editor. After the editor is installed, you need to install the Go application. To install the application, press Ctrl+Shift+X or click on extension. Enter go in the search, select Go, and click Install.

For the go application to work, you need to install additional go libraries. To do this, you need to press Ctrl+Shift+P and write go: install/update tools. Select all libraries from the list and click OK. Vscode output will be produced

Tools environment: GOPATH=/home/maksym/go
Installing 7 tools at /home/maksym/go/bin in module mode.
  gotests
  gomodifytags
  impl
  goplay
  dlv
  golangci-lint
  gopls

Installing github.com/cweill/gotests/gotests@latest (/home/maksym/go/bin/gotests) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (/home/maksym/go/bin/gomodifytags) SUCCEEDED
Installing github.com/josharian/impl@latest (/home/maksym/go/bin/impl) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (/home/maksym/go/bin/goplay) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (/home/maksym/go/bin/dlv) SUCCEEDED
Installing github.com/golangci/golangci-lint/cmd/golangci-lint@latest (/home/maksym/go/bin/golangci-lint) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (/home/maksym/go/bin/gopls) SUCCEEDED

All tools successfully installed. You are ready to Go. :)

Hello World

Now let’s create the first program on go. To begin with, I will create a directory in which the code will be stored

mkdir -p ~/go/src/github.com/mpostument/hello_world

In the hello_world folder, you first need to initialize the go module.

$ go mod init
go: creating new go.mod: module github.com/mpostument/hello_world

In the hello_world folder, you first need to initialize the go module with go mod init command. If the command ends successfully, the file go.mod will appear, which will contain information about all 3rd party libraries used in module. But now this file only contains information about the version of go and the name of the module.

module github.com/mpostument/hello_world

go 1.19

Now you can start writing code. Let’s create a file main.go and add the following code to it

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
}

Since we are not writing a library, but creating executable, the package name should be main. Next, is the libraries import, we need fmt to print text to console. The main function is called as we execute binary. In main function, we print text to the terminal using the Println method from the fmt library.

The code can be run in two ways. Compile the binary or run with go run.

$ go run main.go
Hello World!
$ go build
$ ls
go.mod  hello_world  main.go
$ ./hello_world
Hello World!

Video