Skip to content

Instantly share code, notes, and snippets.

@surjikal
Last active December 20, 2015 22:39
Show Gist options
  • Save surjikal/6206796 to your computer and use it in GitHub Desktop.
Save surjikal/6206796 to your computer and use it in GitHub Desktop.

http://gerg.ca/talks/go-pycon/

var a int = 42
var b int = 24
// := is assignment with type inferrence
c := a + b
  • Slices are like lists
  • First order functions
  • Interface types (abstract classes work like coffeescript, but checked at compile time)

Object Oriented

  • Attach methods from any user-defined types
  • No inheritance, only composition/delegation
  • Interfaces

Instead of delegating to a composed type, like this:

type User struct {
  foo Foo
}

function (u User) DoThingOnFoo() {
  return Foo.bar()
}

You can do this!

type User struct {
  Foo // don't assign a variable name to the type
}

main (u User) {
  u.bar() // automatic delegation
}

Interfaces:

type PrivilegeSet interface {
  HasPrivilege(name string) bool
	SetPrivilege(name string)
}

Go doesn't have exceptions.

  • Return multiple values instead, follow a convention.
  • Use defer instead of try/finally.
  • You can use panic for a super error, like no more memory.
  • Controversial design decision...

Concurrency

  • Called goroutines.
  • They communicate over channels. Channels define a certain type.
  • DONT COMMUNICATE BY SHARING MEMORY, INSTEAD SHARE MEMORY BY COMMUNICATING.
func main() {
	// launch a background goroutine, send an
	// endless stream of requests to it, and
	// print each responses
	data := make(chan string)
	go responder(data)
	for {
		data <- "ping"
		response := <-data
		fmt.Printf("<- %s\n", response)
	}
}
// receive a stream of requests and send appropriate responses
func responder(data chan string) {
	for {
		request := <-data
		fmt.Printf("%s ", request)
		switch request {
		case "ping":
			data <- "pong"
		default:
			data <- "huh?"
		}
	}
}

Aggresively Minimalist

  • New features are shot down on mailing lists
  • If the language doesn't support it, just can build it
  • Use data instead of code to do stuff (use structs, tuples, etc)

Conclusion

  • Not as fun/easy/productive as Python
  • Infinitely better C, Java or C++.
  • "C and C++ don't need to exist anymore"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment