Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC 601 Go Presentation

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

CSC 601 Go Presentation

Presentation on the Go Programming Language by Spencer Egart for CSC 601 - Advanced Programming Workshop

Avatar for Spencer Egart

Spencer Egart

October 29, 2012
Tweet

Other Decks in Programming

Transcript

  1. Background • Developed by Google starting in 2007 • Released

    in an experimental state in 2009 • First stable release (Go 1) in May 2012 • Currently under active development
  2. Why Go? • Aims to provide a C-style language that

    takes cues from languages like JavaScript and Python • Provides the power of a compiled language with the development ease of a scripting language • Cross-platform, open-source
  3. Go Basics • Mostly like C • Automatic semicolon insertion

    at end of line ◦ Enforces curly brace style • Statically typed, with type inference • Allows for multiple returns and assignments • Pointers but no pointer arithmetic • Garbage collected
  4. Simple Data Types bool string int int8 int16 int32 int64

    uint uint8 uint16 uint32 uint64 uintptr byte // alias for uint8 rune // alias for int32 // represents a Unicode code point float32 float64 complex64 complex128
  5. Variables • variable declarations ◦ var x int ◦ var

    foo string ◦ var x, y, z int32, string, float64 • type inference ◦ x := 3 ◦ a, b, c := 1, "okay", true
  6. Data Structures //map of strings to ints mMap := make(map[string]int)

    mMap["one"] = 1 //array of ints, called a slice mSlice1 := []int{2, 3, 5, 7, 11, 13} mSlice2 := make([]int, 5)
  7. Control Structures (for and if) func main() { sum :=

    0 for i := 0; i < 10; i++ { if i % 2 == 0 { fmt.Println(i) } sum += i } fmt.Println(sum) }
  8. Control Structures (for as while) func main() { sum, i

    := 0, 1 for sum < 500 { sum += i i+=2 } fmt.Println(sum) }
  9. Control Structures (switch) func intAsWord(x int) (word string) { switch

    x { case 1: word = "one" case 2: word = "two" default: word = "lots" } return }
  10. Functions func sub(x int, y int) int { return x

    - y } func add(x, y int) int { return x + y } func mult(x, y int) (result int) { result = x * y return }
  11. Closures func adder() func(int) int { sum := 0 return

    func(x int) int { sum += x return sum } }
  12. Structs type Tuple struct { first, second int } func

    main() { foo := Tuple{1, 2} fmt.Printf("%d %d\n", foo.first, foo.second) }
  13. Visibility Uppercase structs, members, and functions are exported, lowercase are

    not. Exported is like public, otherwise everything is private
  14. Structs • Go implements objects as structs with functions •

    Ex: func (t *Tuple) Sum() int { return t.first + t.second } ... foo.Sum()
  15. Inheritance • No explicit inheritance, but anonymous data members allow

    for some form of inheritance (but not polymorphism) type Threeple struct { Tuple third int } func main() { foo := Threeple{Tuple{1, 2}, 3} fmt.Printf("%d %d %d\n", foo.first, foo.second, foo.third) } //prints 1 2 3
  16. Interfaces • Go's interface model acts "backwards" in comparison to

    most OO languages like Java. • After an interface is defined, any struct which implements the defined methods is automatically a member of that interface
  17. Multiple Returns func (this *Tuple) swap() (int, int) { return

    this.second, this.first } a, b := swap(foo) for index, value := range(mArray) { //loop stuff }
  18. Errors and Exception Handling • No Java-like exceptions • Multiple

    returns allow for error codes without the possible ambiguity in older error code implementations file, err := os.Open(path) if err != nil { //handle error } else { //do stuff }
  19. Concurrency with Goroutines • A function foo() can be run

    asynchronously with the keyword "go", ex: ◦ go foo() • Communication between Goroutines occurs over channels ◦ x := <- chan //Receive a value from channel chan
  20. Where is Go? • Internally at Google • Moovweb -

    mobile websites • Heroku - cloud hosting • Canonical - Ubuntu folks • http://go-lang.cat-v.org/organizations-using- go
  21. Summary: The Best Parts • New ◦ Doesn't have all

    the garbage that accumulates in a language over the years ◦ Built from a modern perspective as a general-use language • Feature-rich ◦ Full closures, built-in concurrency support • Minimal, "friendly" syntax • Fast, compiled
  22. Summary: The Worst Parts • New ◦ Less third-party support

    ◦ Relatively fewer developers ◦ Language likely to change within the next few years ◦ Uncertain future • Missing some features ◦ Generics, real inheritance, • Exception model ◦ Error code checking can be just as bad as try/catch ◦ Scraps the idea of exceptions instead of fixing them
  23. Sources & Information http://golang.org/ - Official Site http://tour.golang.org/ - Interactive

    Tour http://www.youtube.com/watch? v=rKnDgT73v8s - Go Programming Language at Google Tech Talks http://golangtutorials.blogspot.com - GoLang Tutorials