loading...

Golang - Getting Started

codehakase profile image Francis Sunday Updated on ・3 min read

“Go will be the server language of the future.” — Tobias Lütke, Shopify

Go was first created as an experiment, the goal of its creators was to come up with a language that would resolve bad practices of others while keeping the good things.

Its first release was on March 2012. Go was designed to feel familiar and to stay as simple as possible, the entire language specification fits in just a few pages.

What's this series about?

This series is a gentle introduction to the Go programing language, covering the basic concepts, syntax and features of Go. The ending parts of the series would focus on building from Zero to Live app using the Go programming language.

Why Go?

Why would you choose Golang over tonnes of other languages such as python, ruby, nodejs, etc?

Here are some of the pros I've seen in Go:

  • Concurrency is an inherent part of the language. As a result writing multi-threaded programs is a piece of cake. This is achieved by Goroutines and channels (will be discussed fully in next parts of this series).

  • Golang is a compiled language. The source code is compiled to native binary. This is missing in interpreted languages such as JavaScript used in nodejs.

  • The language spec is pretty simple. The entire spec fits in a page and you can even use it to create your own compiler... sweet isn't it?

Installation && Setup

Golang is supported on all the three platforms Mac, Windows and Linux. You can download the binary for the corresponding platform here - https://golang.org/dl .

Linux setup

Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go.

tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

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

Mac OS X package installer

Download the package file, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go.

The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

Windows installation

Open the MSI file and follow the prompts to install the Go tools. By default, the installer puts the Go distribution in c:\Go.

The installer should put the c:\Go\bin directory in your PATH environment variable. You may need to restart any open command prompts for the change to take effect.

Testing our Installations

To test if Go is properly installed on our machine, we need to create a workspace, and write our first program.

We'll first create our workspace:

cd $HOME/go && mkdir test

Next we create our source file test.go with the following content:


package main 

import "fmt"

func main() {

  fmt.Println("Hello World!")
}

The code above, represents a simple Go program. Every Go program is made up of packages. Our program, starts running in the package main. This program is using the package with import paths "fmt".

By convention, the package name is the same as the last element of the import path. For instance, the fmt package comprises files that begin with the statement package fmt.

To build the program, Go provides a build tool for us to use:

cd $HOME/go/test

go build test.go

The command above will build an executable named test in the directory alongside the source code. Execute it to see the greeting:

./test
Hello World!

If you see the greeting, you've been successful so far, and your Go installation works.

This is the first article in the Golang tutorial series. In the next articles, I'll be covering the following topics:

  • Variables - VIEW
  • Types - VIEW
  • Constants - VIEW
  • Functions - VIEW
  • Packages
  • Conditional Statements and Loops
  • Arrays, Slices and Variadic Functions
  • Pointers, Structures and Methods
  • Interfaces
  • Concurrency
  • Object Oriented Programming
  • Defer and Error Handling

I hope you've enjoyed this part of the series? You can read other articles by me on my Blog or on The Practical Dev.

If I've missed anything, let me know in the comments.

Discussion

pic
Editor guide
Collapse
levi_zitting profile image
Levi Zitting

I've been wanting to get into Go for awhile now. This series sounds like the perfect way to help me get into it over time.

Anyways, fantastic part one, and I look forward to reading the rest of the series!

Collapse
codehakase profile image
Francis Sunday Author

Thanks, I'm glad you're liking the series so far, I promise subsequent articles, would broaden your knowledge of the Go programming language.

Collapse
k4ml profile image
Kamal Mustafa

Shouldn't you set GOPATH as well ?

I made this year resolution to learn Go but look like I won't be achieving that, given I have 1 month left 😂 . But I'm still enjoying playing around with stuff written in Go.

This is my notes on setting up Go environment on Ubuntu, based on howistart article which I think the best getting started guide for new language you want to learn.

Collapse
codehakase profile image
Francis Sunday Author

You're right Kamal, I think I mentioned setting up GOPATH (maybe indirectly), anyways I'm glad you chose to learn Go.

Collapse
etcwilde profile image
Evan Wilde

Overall, good article on getting started with Go.

I don't know that I would call ~90 pages a "few" though.
Current spec that I'm referring to; golang.org/ref/spec

Granted, it's a lot shorter than the ~1300 page monstrosity of the current C++ spec, but comparatively long next to the 60-page spec for R (which I actually think is a little lacking).

Collapse
codehakase profile image
Collapse
iridakos profile image
Lazarus Lazaridis

There's a typo in the post's description (Langugae -> Language).
It's visible when you share the post.

Nice post!

Collapse
codehakase profile image
Francis Sunday Author

Thanks for pointing that out Lazarus

Collapse
pusakat profile image
eric pareja

Minor typo: "create our source file hello.go" should be "create our source file test.go"

Collapse
codehakase profile image
Francis Sunday Author

Thanks for pointing that out Eric 3>