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

Golang for Rubyists

Golang for Rubyists

Nihad Abbasov

April 13, 2019
Tweet

More Decks by Nihad Abbasov

Other Decks in Programming

Transcript

  1. Go’s Creators • Ken Thompson (B, C, Unix, UTF-8) •

    Rob Pike (Unix, UTF-8) • Robert Griesmer (V8, HotSpot, JVM)
  2. “Go at Google: Language Design in the Service of Software

    Engineering” https://talks.golang.org/2012/splash.article SECTION TWO
  3. Key points • must work at scale • must be

    familiar, roughly C-like • must be modern
  4. Enter Go + Fast: fast compilation like interpreted language +

    Safe: strongly and statically typed and garbage collected + Easy: concise, easy to read + Modern: built in support for multi-core networked distributed applications
  5. Data Types • Boolean types: true, false • Numeric types:

    integers, floats • String types • Derived types: pointers, arrays, structs, functions, slices, interfaces, maps, channels
  6. VARIABLES var x float64 x = 20.0 // same as

    above var x float64 = 20.0 foo := 42 var a, b, c = 3, 4, "foo" Static Declaration Dynamic Declaration Mixed Declaration
  7. CONDITIONALS if 7%2 == 0 { fmt.Println("7 is even") }

    else { fmt.Println("7 is odd") } if/else if 8%4 == 0 { fmt.Println("8 is divisible by 4") } if statement without an else
  8. CONDITIONALS t := time.Now() switch { case t.Hour() < 12:

    fmt.Println("It’s before noon") default: fmt.Println("It’s after noon") } switch
  9. LOOPS i := 1 for i <= 3 { fmt.Println(i)

    i = i + 1 } single condition for i := 0, i < 3; i++ { fmt.Println(i) } initial/condition/after
  10. SLICES s := make([]string, 3) s[0] = "a" s[1] =

    "b" s[2] = "c" s = append(s, "d", "e")
  11. FUNCTIONS func FunctionName([parameter_name type]) [return_types] { // body of the

    function } func plus(a int, b int) int { return a + b } sum := plus(1, 2)
  12. GOROUTINES func f(n int) { for i := 0; i

    < 10; i++ { fmt.Println(n, ":", i) } } func main() { go f(0) var input string fmt.Scanln(&input) }
  13. CHANNELS func main() { messages := make(chan string) go func()

    { messages <- "ping" }() msg := <-messages fmt.Println(msg) }
  14. STRUCTS type Person struct { name string age int }

    p := Person{"John", 20} fmt.Println(p.name)
  15. INTERFACES type Animal interface { Speak() string } type Cat

    struct { } func (c Cat) Speak() string { return "Meow!" }
  16. RESOURCES A curated list of awesome Go frameworks, libraries and

    software https://github.com/avelino/awesome-go https://awesome-go.com/
  17. RESOURCES “Goby is an object-oriented interpreter language deeply inspired by

    Ruby as well as its core implementation by 100% pure Go” https://github.com/goby-lang/goby