Contents

Overview

go build [-i] github.com/user/aclient/cmd/multiget //  (-i installs binary - like running go install) 
go clean [-i] github.com/user/aclient/cmd/multiget // removes object files and executables (-i removes binaries installed with go install) 

Go+git

git clone https://github.com/origin/repo.git OR go get https://github.com/origin/repo

git remote add fork https://github.com/fork/repo.git
git push fork

This is preferred to cloning the fork because it enables pushing changes to your fork without changing import paths. So for instance if there are local import paths like github.com/origin/repo/sub then you don't have to change them all to github.com/fork/repo/sub because you're working on the origin.

Sending pull requests from the fork then becomes easy because the package import paths are same as for the origin.

tldr; don't clone or work on the fork - simply add it as a remote and push changes to it. Anyway in most cases you won't be able to push changes to the origin because you won't have permission.

Named types

type StringSlice []string

Methods can be defined for any named type (except a pointer or an interface); the receiver does not have to be a struct. So:

func (s *StringSlice) Len() int {
  return len(s)
}

Concurrency

Share memory by communicating.

channels
  • Read channel:
var ch <-chan int
ch := make(<-chan int)
  • Write channel
var ch chan<- int
ch := make(chan<- int)
  • Channels are usually declared bidirectional. Read/write channels are useful as arguments / return values from functions. So for instance:
func returnReadChannel() <-chan {
  ch := make(chan int, 1)
  go func() {
    defer close(ch)
    //do something else
  }
  return ch // ch gets converted to a read channel when function returns so caller of function can only read from channel
}
  • Buffered
bch := make(chan<- int, 2)
  • Range over channel (or read channel) doesn't block if channel is closed.
for x := range ch { // x has zero value of ch type
  // 
}
x, _ := <-ch  //doesn't block if ch is closed. x has zero value of ch type
  • Reading from closed channel results in zero value of channel type. Can use the untyped bool value returned to check whether channel closed (false).
if val, ok := <-ch; ok {    // ok is false if ch is closed
  // do something with val
}
  • Sending on closed channel panics.

  • Sending on nil results in fatal error.

Builtin

append

s := make([]int, 5)
s = append(s, 6) // Out: [0,0,0,0,0,6]

sort

A type, typically a collection, that satisfies sort.Interface can be sorted by the routines in the sort package. The methods require that the elements of the collection be enumerated by an integer index.

type Interface interface {
    // Len is the number of elements in the collection.
    Len() int
    // Less reports whether the element with
    // index i should sort before the element with index j.
    Less(i, j int) bool
    // Swap swaps the elements with indexes i and j.
    Swap(i, j int)
}

signal

The signal package implements access to incoming signals.

Signals are a form of inter-process communication (IPC) mediated by the kernel and handled by processes/threads. They are asynchronous messages sent to processes or specific threads. Signals are similar to interrupts but differ from interrupts which are mediated by the processor and handled by the kernel.

The following signals cannot be caught by Go programs: + SIGKILL + SIGSTOP

The following signals are synchronous (triggered by program execution errors) and will get converted to run time panic by Go programs. + SIGBUS : process causes bus error, i.e. process attempts to access non-existent physical address. + SIGFPE : process attempted erroneous arithmetic operation. FPE (floating point exception) is misnomer because also sent if process attempts integer divide by zero on x86 CPU. + SIGSEGV : process attempted to access memory address outside of it's virtual memory space.

The following signals are asynchronous (sent by kernel or another program). + SIGHUP : when programs loses it's controlling terminal + SIGINT : Control-C (interrupt character) sent - program exits + SIGTERM : Control-\ sent - program exits with stack dump

os.signal.Signal.Notify(chan<- os.Signal, sig ...os.Signal) can be used to over-ride default behaviour and deliver signals over registered channels. Notify must use a buffered channel or risk missing the signal if program isn't ready to receive when the signal is sent because Notify doesn't block and simply drops the signal.

  // send but do not block for it
  select {
    case c <- sig:
    default:
  }

A signal mask will block certain types of signals and prevent a Go program from responding to these signals. Some signals cannot be blocked using masks: + SIGILL, SIGTRAP, SIGSTKFLT, SIGCHLD, SIGPROF, SIGCANCEL, SIGSETXID (last 2 used internally by glibc).

When Go programs write to a broken pipe, kernel will raise signal: + SIGPIPE

If no channel has been registered to be notified of SIGPIPE, the program will exit if the write is to a file descriptor of type 1 or 2. Write to all other types will cause write to fail with EPIPE error.

There are precautions while handling signals when calling non-Go programs from Go programs and vice-versa. Check os.signal for details.

Modules

go list -f '{{ .Dir }}' -m all
go list -f '{{ .Dir }}' -m github.com/golang/protobuf // /home/sridhar/dev/mygo/pkg/mod/github.com/golang/protobuf@v1.2.0
cd ~/dev/modgo/src/github.com/agnivade/funnel
go build -tags "disableelasticsearch disableinfluxdb disablekafka disableredis disables3 disablenats" ./cmd/funnel // outputs funnel binary in pwd
go install ./cmd/funnel // installs funnel binary in $GOPATH/bin

Testing

go test -run Foo github.com/user/project/

Tool

go tool # lists all available tools
  • addr2line
  • asm
  • buildid
  • cgo
  • compile
  • cover
  • dist
  • doc
    • go doc pkg # shows documentation for pkg
    • go doc net/http Server.Close # shows documentation for method Close() of Server in net/http
    • go doc cmd/ # shows documentation for specific tool
  • fix
  • link
  • nm
  • objdump
  • pack
  • pprof
  • test2json
  • tour
  • trace
  • vet

Gotchas

References