DEV Community

Cover image for Getting Going with Go, Day 1
Kai
Kai

Posted on

Getting Going with Go, Day 1

The Why of this exercise

Yesterday, on my birthday, I decided to start documenting my daily progress on exploring a new programming language: Go. It’s not going to be quite a #100DaysofCode, as I aim to have it be more concise than a hundred days, but I will try to document every day’s progress, no matter how small or inconsequential-seeming at the time. I will include references to materials I used, as well as further reading at the bottom of each article (I did spend years in academia, after all…) so hopefully you will find something of interest there.

This is the first instalment, where we get set up and ready to Go. I am using a Mac, so any instructions and code below will be from that vantage point, but similar commands will exist for Linux and other operating systems as well. But first, a bit on the dual reasons I am pursuing this GOal (and why you have to deal with these strained puns).

Go, sometimes referred to in hashtags as Golang, is a modern programming language originally developed at Google to take in some of the best parts its designers liked in other programming languages, namely static typing, high performance and usability. As a project, it tries to marry the three strands of programming not often seen as available together: "One had to choose either efficient compilation, efficient execution, or ease of programming; all three were not available in the same mainstream language". So we’re looking at a robust and performant language that aims to not be esoteric or demand deep knowledge of issues such as memory management and garbage collection from a programmer. Sounds good.

I am a relatively newly-minted software developer. On a daily basis, I chiefly work with Ruby (and Ruby on Rails) meaning I am used to working with an interpreted language, duck typing, a big friendly framework with lots of syntactic sugar giving me nifty helpers, and a robust codebase in my workplace’s application to which I can refer and from which I can crib.

The chief focus of the company I work for is managed Kubernetes. While I haven’t worked on the underlying infrastructure layer or the operators that make our particular offering work, both they and Kubernetes itself are written in Go. You can see where this is going: I want to be able to understand what is going on when I peek behind the curtain, as well as maybe in the longer term even be able to contribute!

So, to summarise: I am looking to improve my knowledge of different types of programming languages. With Go being statically typed and compiled, as well as both well-documented and the language used in an element of what my company works with, it makes sense to pick it for study. Nothing quite like diving in head-first, right? At least this way the worst we can do is write programs that just don’t work, rather than hurt ourselves.

Installing Go

As mentioned, I’m on a Mac, so I was able to install the tools to build Go code into programs using Homebrew: brew install go. If you are on another operating system, or are not using Homebrew, your first port of call should be the binary distribution for your OS.

At the time of writing this, brew installed version 1.15 of Go:

$ brew install go
==> Downloading https://homebrew.bintray.com/bottles/go-1.15.5.mojave.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/70969a6147bad6960136bdea78063d39b527c993a725ad525a3cf6e9f8ad6
######################################################################## 100.0%
==> Pouring go-1.15.5.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/go/1.15.5: 9,784 files, 494.2MB
Enter fullscreen mode Exit fullscreen mode

Let’s make sure everything is set up. If the following command does not work for you, you may need to set your $GOPATH variable to make sure your operating system will know where to look for Go when you run it. Try running go in your home directory:

kai:~ $ go
Go is a tool for managing Go source code.

Usage:

    go <command> [arguments]

The commands are:

    bug         start a bug report
    build       compile packages and dependencies
    clean       remove object files and cached files
    doc         show documentation for package or symbol
    env         print Go environment information
    fix         update packages to use new APIs
    fmt         gofmt (reformat) package sources
    generate    generate Go files by processing source
    get         add dependencies to current module and install them
    install     compile and install packages and dependencies
    list        list packages or modules
    mod         module maintenance
    run         compile and run Go program
    test        test packages
    tool        run specified go tool
    version     print Go version
    vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

    buildconstraint build constraints
    buildmode       build modes
    c               calling between Go and C
    cache           build and test caching
    environment     environment variables
    filetype        file types
    go.mod          the go.mod file
    gopath          GOPATH environment variable
    gopath-get      legacy GOPATH go get
    goproxy         module proxy protocol
    importpath      import path syntax
    modules         modules, module versions, and more
    module-get      module-aware go get
    module-auth     module authentication using go.sum
    module-private  module configuration for non-public modules
    packages        package lists and patterns
    testflag        testing flags
    testfunc        testing functions

Use "go help <topic>" for more information about that topic.
Enter fullscreen mode Exit fullscreen mode

Setting up our IDE for Go

The next thing is to prepare my editor to deal with Go. I am using Microsoft’s VSCode, which has a big plug-in/extension for Go available, along with lots of other extensions that I might explore later. Go code can be edited in any text editor, though, so go wild with whatever you’re comfortable with.

VSCode Go extension

You can get the official VSCode Go extension by searching for “go” in the extensions menu, or from this site. I am especially looking forward to utilising the IntelliSense code completion suggestions and the integrated linter.

That’s it for today. Coming up next: Taking our setup for a spin and writing our first lines of Go!

References and further reading

Top comments (0)