Day 20: Hello Go — Quickstart Guide


Project (30 mins): Quick and easy installation of “Go” and using it for creating your very first Go app.

All the code for this project can be found on my github

Step 1: Install

Use this link to install Go lang on your machine. Check by running go on the terminal which should give out usage like below.

Step 2: Setup Workspace

Go commands all rely on one important environment variable called $GOPATH which points to the Go workspace (note this is different from $GOROOT which tells where Go is installed). Set it up as follows in Unix-like systems;

export GOPATH=/Users/Harini/Go

Step 3: Create simple app

  • In $GOPATH, create three folders: src, pkg, bin
  • Create a package directory called “mymath” in src dir and add a “sqrt.go” file to it as follows:
  • Compile package using the following command which will generate “mymath.a” file in pkg directory.
cd $GOPATH/src/
go install mymath
  • Now create an app directory called “mathapp inside src directory. Add a file called “main.go” to it with the following code:

Step 4: Build and Run

Build the go file to create an executable file called “mathapp” inside bin directory using the following command:

cd $GOPATH/src/mathapp
go install

Now running the executable mathapp should show you the following:

Hello world. Sqrt(2) = 1.414213562373095

Day 20 of #100DaysOfCode DONE

If you enjoyed this, please click 👏 so that others can enjoy it as well. Follow me on Twitter @HariniLabs to get the latest updates or just to say Hi :)

PS: I curate a bi-weekly #WomenInTech newsletter for a dose of inspiration from the world of tech and yes men can signup too! Get it here :)