Gooooo(al)!

Toby DiPasquale / @codeslinger

Yet another programming language?

A systems language out of Google designed for

  • speed
  • simplicity
  • scale

Speed

Native compilation

  • amd64
  • x86
  • ARM
  • cross-compilation quite easy

Very fast builds

Standard profiling and benchmarking tools

Simplicity

Straightforward C-like syntax

Interfaces over inheritance

First-class concurrency

Scale

Canonical build tools

Canonical maintenance tools

Static linking

Features

Strongly typed


 var arg uint32

 //...

 // explicit casts only allowed in Go
 fmt.Printf("arg as int = %d", arg.(int))
          

Value types


 // this takes up the amount of space that you'd expect
 type TCPPacket struct {
     SrcPort  uint16
     DestPort uint16
     Seq      uint32
     Ack      uint32
     Flags    uint16
     Window   uint16
     Csum     uint16
     Urgent   uint16
     Option   uint32
     Data     []byte
 }
          

Data structures

  • Array
  • Slice
  • Map
  • Linked list
  • Suffix array
  • Ring buffer
  • Heap

Multiple return values


 type Boxofjunk struct {
     f *os.File
 }
 
 func NewBoxofjunk(file string) (*Boxofjunk, error) {
     f, err := os.Open(file)
     if err != nil {
         return nil, err
     }
     b := new(Boxofjunk)
     b.f = f
     return b, nil
 }
          

Case-based exports


 // this function is visible to all packages
 func PublicFunction(arg string) error {
   // ...
 }

 // this function is only visible within its package
 func internalFunction(arg string) error {
   // ...
 }
          

Garbage collected

No exceptions (sort of)

Composition over inheritance

  • No classes
  • No subtype inheritance
  • Structural typing only
 package io

 type Reader interface {
   Read(p []byte) (n int, err error)
 }

 //...

 package junkatronics

 type MyStuff interface{}

 // MyStuff now satisfies the io.Reader interface
 func (m *MyStuff) Read(p []byte) (n int, err error) {
   //...
 }

Embedding


 type MyReadWriter interface {
     io.Reader
     io.Writer
 }

 var b []byte
 //...
 _, err := myReadWriter.Read(b)
          

CSP-style concurrency


 listener, err := net.Listen("tcp", ":3456")
 if err != nil {
     log.Fatalln(err)
 }
 for {
     conn, err := listener.Accept()
     if err != nil {
         log.Println(err)
         continue
     }
     go handleConnection(conn)
 }
          

Channels


 func handleSignals(exitChan chan int) {
     signalChan := make(chan os.Signal, 1)
     signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGQUIT)
     go func() {
         <-signalChan
         exitChan <- 1
     }()
 }
 //...
 handleSignals(exitChan)
 select {
 case shutdownNow := <-exitChan:
     // handle shutdown
 }
          

 // Unbuffered channels
 // - sends block until received
 syncChan := make(chan int)
 syncChan2 := make(chan int, 0)

 // Buffered channel
 // - sends don't block unless channel is full
 asyncChan := make(chan int, 100)
          

 countingSem := make(chan int, MaxInFlight)

 func handle(r *Request) {
     countingSem <- 1  // Wait for slot to free up
     process(r)
     <-countingSem     // Free up slot for next request to run
 }

 func Serve(queue chan *Request) {
     for {
         req := <-queue
         go handle(req)
     }
 }
          

Panic / Defer


 func DoStuff(path string) error {
     f, err := os.Open(path)
     if err != nil {
         return err
     }
     defer f.Close()
     //...
     if somethingBadHappened() {
         panic("crazy stuff happened!")
     }
     //...
 }
          

Rich standard library

  • Compression (tar,zip,bzip2)
  • Crypto/TLS
  • HTML templating
  • Image manipulation
  • HTTP
  • SMTP
  • JSON
  • Text parsing and Unicode
  • ...and much more!

Bugs

Unused variables/imports are errors

Tools assume single massive repo

No concept of versioning

FIN