OpenWest 2014/Python to Go

From Omnia
Jump to navigation Jump to search

by Brian G. Merrell

--

Compile first:

go help

test.go:

package main

import "fmt"

func main() {
  fmt.Println("test")
}

go build -o test test.go

To just run the script:

go run test.go

--

Justifications:

  • C++ too complex
  • compilation too slow
  • programming to difficult
  • programmers choosing easy over safety and efficiency

--

Python bad because:

  • actually quite complex
  • runtime errors
  • performance
  • doesn't scale

--

Go has:

  • concurrency is a breeze
  • no exceptions
  • no type hierarchies
  • no classes

Go good because:

  • small language (fit all in your head, have read the spec)
  • compile time checks (fast)
  • fast enough for even more things
  • benefits at little cost
  • dynamic feel with compiled performance
  • rich standard library
  • libraries are all written in Go
  • great bundled tooling
  • seems to scale well
  • cross compiling in Go is simple
  • Go syntax is very light and simple

--

Hello World Web server:

package main

import (
 "fmt"
 "net/http"
)

func main() {
 http.HandleFunc("/", Hello")
 http.ListenAndServer("kicakhost:800", nil)
}

func Hello(w http.ResponseWrite, r *http.Request....
???

--

Don't like about Go:

  • Naked return statements (optional)
x,yz, =1,2,3
return
  • scoping rules specific case
  • not faster than java
  • new vs &T{} vs make
  • len("foo") vs "foo".Len() - but keeps lanuage simple
  • Name "Go" is ungoogable / hastagable (use golang) *** MOST ANNOYING ***
  • No dynamic linking in Go - makes binaries huge
    • two compilers - may work with gccgo, just not with google's gc compiler
    • no dlopen in Go

Python vs Go

  • Interpreted vs Compiled
  • Strongly typed vs Strongly typed
  • Dynamically typed vs statically typed
  • Garabage collected
  • Unique syntax vs Similar to C
  • Open Source
  • Python Foundation vs Google
  • more platforms vs fewer platforms
  • 1991 vs
  • ???

Go does not have:

  • Classes
  • Exceptions
  • Assertions
  • Templates / generics (for now)
  • Operator overloading

Zen of Python

>>> import this

Go does not have:

  • decorators
  • static methods
  • class methods
  • properties
  • iterators
  • generators
  • exceptions
  • meta classes
  • class decorators
  • multiple inheritance
  • list comprehensions
  • ternary operator (expression?true:false)

--

Errors should never pass silently - sure beats runtime error

package main

import (
 "fmt"
 "net/url"
)

func main() {
 u := url.Parse("http://foo.org/index%.html") // notice bad '%'
 /* fix
 u, err := url.Parse("http://foo.org")
 if err != nil {
   fmt.Println("got error")
 }
 */
 fmt.Println(u.Host)
}

Zero Values

Encapsulation - capitalized names are exported

Slices vs Lists

Arrays

Slicing syntax is very similar to python

Maps, Dicts and Sets

Duck typing - Go uses interfaces to solve

Exception handling - uses interfaces and type assertions to check

Calling C code from Go

go get - similar to pip

Unit testing and Code coverage

go test ./...
go test -cover ./...

Other tools (included)

http://play.golang.org