trying to go

I’m trying to learn golang1 whether I’m ready or not.2 I’m planning to write about what I learn here–mostly to help myself move forward. I’m also going to try to learn cli-based git at the same time. I’ve setup a repository here for my first little golang project–a recreation of the functionality of my mastodon-based @dicewarebot (likely without the final API-push to mastodon).

Why Rolling Dice?

The mastodon dicewarebot was the first project I undertook in python to really try to understand what was going on with the language. It was also really fun! I figure a similar project in golang will help me wrap my head around a smaller project to familirize myself with the syntax and general workings of the language.

Additionally, I’m a fan of the EFF and strong passwords. This project will (again) use EFF’s updated long word list.

Alt text

NOTE: I have very little idea about what I’ll be doing, so I’m going to write up generally what I’ve done and what I’ve learned.

Installation

Get go installed by following their instructions. Pretty straightforward.

  1. Download golang
  2. Install golang
Directory Structure

Interestingly, go enforces (or at least encourages) a directory-based organization for your source code and your eventual binary builds. So all of the source code for any of your work is contained in organized folders in–for example–$HOME/go/src/, and issuing the following command will compile the program to binary file in $HOME/go/bin

$ go build

So if you have a program called test.go in the $HOME/go/src/test folder, you can quickly move into the test folder:

$ cd $HOME/go/src/test

build the program3:

$ go build

and then run the newly compiled program from $HOME/go/bin by

$ cd $HOME/go/bin/
$ ./test

I’ve never really compiled my programs before. It’s really fast because my programs are small. It’s also really interesting and the whole setup feels very clean. I like it.

Learning

You don’t even have to install go to start learning if you don’t want to. Whether you’ve chosen to install locally or not the best place to start learning seems to be A Tour of Go–the interactive environment at the official go page. There’s also a How to Write Go Code document that looks very good as well (though it’s short). The longer Effective Go may be better because it has much more detail.

While I was planning on working my way completely through A Tour of Go to start out (I’ll get there eventually) I ended up getting to work on writing code that actually worked. The below (working golang code) is a handy description of what I’ve learned about the general structure of the language so far.

package main 
// Comments begin with two forward slashes
// Import pulls in additional packages 
import (
   "fmt" // Info: https://golang.org/pkg/fmt/
)

// Var is pre-defined variables
// Though they can also be set inside "func main()" below
var (
// [var name]	[type]	= [value]
DiSides	int	= 8
DiNum	int = 10
)

// Functions are snippets of code you create for yourself.
func add(x , y int) int {
	return x + y
}
func multiply(x , y int) int {
	return x * y
}

// Inside of "func main() " seems to be where
// you write the code your script will execute and run.
func main() {
// Show off some variables
fmt.Println("This is the variable DiNum:\t", DiNum)
fmt.Println("This is the variable DiSides:\t", DiSides,"\n")
// Show off some functions
fmt.Println("Add the variables with our function:\t", add(DiNum,DiSides))
fmt.Println("Multiply the variables with our function:\t", multiply(DiNum,DiSides),"\n")
// Plain Strings
fmt.Println("Hello, Golang!")
fmt.Println("I must be learning something about you")
fmt.Println("Though it's not yet enough to roll dice...\n")
// And multiline strings with the backtick
fmt.Println(`Being totally honest--golang is really fun.
It forces you to get things right (or it won't run)
and it forces you to use packages you import (or it won't run)
and while that sounds like a pain (and it is frustrating)
it's clarifying because it forces you to be explicit.`)
}

I’ve made some additional progress tonight on the script up at github. I’m confident there’s a better way to do everything I’m doing, but I’m learning–and learning is good. Let’s see what I can figure out in a little more time.

good ideas


  1. Thank you to Máirín Duffy for helping me broaden my self-perception a bit. She’s responsible for me starting to learn golang. [return]
  2. I started out programming with #bash and #python. I’ve learned some SQL-like syntax for working in Google sheets4, which I then used locally after importing some *.csv files to a local database (and the sytax is familiar enough to work there too). Some #awk and others I’m forgetting–nearly 10 years of Linux desktop use by choice will get you familiar with some things. [return]
  3. see How does the go build command work by Dave Cheney for more depth than I understand–though it’s very readable. [return]
  4. Yes, google sheets has a built-in query function. It’s actually quite powerful, more-so than the brief link would indicate. Short version: I’ve outstripped the tool and need to explore other options (like R and the pandas module for python). [return]