Skip to content

Instantly share code, notes, and snippets.

@rkaneko
Last active August 29, 2015 14:05
Show Gist options
  • Save rkaneko/19bcedb02bef3a35de13 to your computer and use it in GitHub Desktop.
Save rkaneko/19bcedb02bef3a35de13 to your computer and use it in GitHub Desktop.
My notes on exercises a tour of Go.

Getting started My Go lang

see:

env

MacOSX

Install

$ brew install go

$ go version

# for local go repository
$ mkdir ~/go

open .bashrc

#go lang
export GOROOT=`go env GOROOT`
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Hello Go!

$ mkdir -p $GOPATH/src/github.com/rkaneko/hello
  • $GOPATH/github.com/rkaneko/hello/hello.go
package main

import "fmt"

func main() {
    fmt.Printg("Hello, world.\n")    
}
$ cd $GOPATH/src

$ go run github.com/rkaneko/hello/hello.go
Hello, world.

go build/install

$ cd $GOPATH/src

$ go build github.com/rkaneko/hello

# If you want add hello binary to $GOPATH/bin, type under.
$ go install github.com/rkaneko/hello

$ tree $GOPATH
├── bin
│   └── hello
├── pkg
│   └── darwin_amd64
│       └── github.com
│           └── rkaneko
└── src
    └── github.com
        └── rkaneko
            ├── hello
                └── hello.go

$ hello
Hello, world.

testing

  • $GOPATH/src/github.com/rkaneko/newmath/newmath_test.go
package newmath

import "testing"

func TestSqrt(t *testing.T) {
    const in, out = 4, 2
    if x := Sqrt(in); x != out {
        t.Error("Sqrt(%v) = %v, want %v", in, x, out)
    }
}
$ go test github.com/rkaneko/newmath
ok      github.com/rkaneko/newmath  0.021s

# see more information for testing
$ go help test

Utility for Vim

  • nsf/gocode
  • golint

TODO

vim-goがあればいらないかも


A tour of go

brand-new things for me

  • (13) Short variable declarations
  • (15) Type conversions (more simply)
    • Unlike in C, Go doesn't convert type implicitly.
  • (30) The new funcion
  • (36) Range
    • Iterate over a slice or map
  • (42) How to see if map's key or value exists or not
  • (49) Switch with no condition
    • convenient when u writes long if then else chains
  • (52) Methods
    • put receiver between func keyword and method name
  • (54) Methods with pointer receivers
  • (57) Errors
  • (63) strings.NewReader("some string"), io.Copy(os.Stdout, pointer of []byte)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment