Go

package main

import "fmt"

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

Table of Contents

  1. Why use Go?
    1. Resources
    2. Setup
  2. The Basics
  3. Compiling Executables for Linux, OSX, Win.

Why use Go?

Go is a fast, compiled language with focus on multicore and networked programs. I’ve done a ton of small, fun projects in NodeJS, and love the ease with which I can express small ideas. Unfortunately, I’ve grown out of Node, and am looking for something with better security and scalability. I’ll be writing personal projects in go for the next few months. With Go 2 on the horizon, now seems like a reasonable time to begin learning.

Resources

  1. Practical GO

Setup

apt install golang

Add to ~/.profile:

export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

The Basics

The first demo in A Tour of Go gives us insight into how go compiles, links and runs. It should be a lot faster to prototype in go than in c.

package main

import (
  "fmt"
  "time"
)

func main() {
  fmt.Println("Welcome to the playground!")

  fmt.Println("The time is", time.Now())
}

  1. The package of the program is main.
  2. A function import() is used to bring in packages fmt and math.
  3. The main() function (hm, no return type?) is executed when the program is run.
  4. fmt.Printf() works similarly to c’s printf.

Interesting things to note:

  1. functions are exported (public) if the name starts with a capital. For instance, Parse() will be available from outside our package, but parse() will be private.

Compiling Executables for Linux, OSX, Win.

An article on dave.cheney.net and a stack overflow thread


CC BY-SA 4.0 - This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. You are free to share, redistribute and adapt the material as long as appropriate credit is given, and your contributions are distributed under the same license.