gomud/docs/learning.md

1.4 KiB

learning

stuff we learned along the way

2021-12-28

Stuff i learned about go

  • := short assignment, infers type: x := 4. vs "long" assignment: var x int; x = 4

  • expressive ifs: declare, assign, evaluate

    if x := isBar(); x > 10 {
      println("big number")
    } else {
      println("it's kind of small")
    }
    
  • defer wrapup (e.g. conn.Close())

  • data structures are array (fixed), slice (variable), and channel

  • mulitiple returns. common convention:

    foo, err := bar()
    if err != nil {
      return err
    }
    
  • pointers 😬

  • use make to make arrays, slices, channels

  • for is the only loop.

  • structs and interfaces. here's a pair type:

    type pair struct {
      x, y int
    }
    

    and here's a toString interface:

    type Stringer interface {
      toString() string
    }
    

    and here's adding a method to a struct to implement the interface:

    func(p pair) toString() string {
      return fmt.Sprintf("(%d, %d)", p.x, p.y)
    }
    

    and here's using the compiler to check the interface:

    var p Stringer = pair{3, 4}
    

resources