I'm just beginning to learn Go coming from a background where I write mainly JavaScript and Java code on a daily basis.

I'll try to document the learning resources I'm utilizing as well as the road blocks I'll face along the way.

Learning Resources

Videos

Websites/Articles

Running a sample application

Following some code samples presented in A Tour of Go I ran the example where Russ Cox demonstrated the power of Reflection.

To run Go on my environment I'm utilizing docker

  • docker run -t -i -p 6000:6000 diogogmt/golang:1.4.2 /bin/bash

main.go

import (  
  "io"
  "io/ioutil"
  "os"
  "fmt"
  "encoding/json"
  "log"
  "time"
  "net/http"
)

type Lang struct {  
  Name string
  Year int
  URL string
}

func main() {  
  start := time.Now()
  do(func(lang Lang) {
    count(lang.Name, lang.URL)
  })
  fmt.Printf("%.2fs total\n", time.Since(start).Seconds())
}

func do(f func(Lang)) {  
  input, err := os.Open("/root/lang.json")
  if err != nil {
    log.Fatal(err)
  }
  dec := json.NewDecoder(input)
  for {
    var lang Lang
    err := dec.Decode(&lang)
    if err != nil {
      if err == io.EOF {
        break
      }
      log.Fatal(err)
    }
    f(lang)
  }
}

func count(name, url string) {  
  start := time.Now()
  r, err := http.Get(url)
  if err != nil {
    fmt.Printf("%s: %s", name, err)
    return
  }
  n, _ := io.Copy(ioutil.Discard, r.Body)
  r.Body.Close()
  fmt.Printf("%s %d [%.2fs]\n", name, n, time.Since(start).Seconds())
}

lang.json

{"Name": "Erlang", "Year": 1986, "URL": "http://erlang.org/"}
{"Name": "PHP", "Year": 1995, "URL": "http://php.net/"}
{"Name": "Scala", "Year": 2003, "URL": "http://www.scala-lang.org/"}
{"Name": "Go", "Year": 2009, "URL": "http://golang.org/"}

Running the program: go install tour && tour

Findings

  • Shift from an object oriented hierarchical to functional based way of thinking
  • Using case notation to define private and public functions -- functions starting with upper case letters are exported
  • Declaring and assigning a value to a variable using the := notation
  • Type inference when declaring variables which makes the language feel really dynamic
  • Descriptive error messages informing compilation problems
  • No parenthesis in control structures
  • Support for providing functions as arguments to functions which reminds me much of JavaScript
  • Very clear way to download and import packages to a project
  • Focus on testing and documentation at a language level

Next I'll be continuing with the examples in the Tour of Go video and add some concurrency features to the little web crawler listed above