Go - Basics, Variables, and Types

Contemporary Programming Languages - CS2001 - 24 October 2017

Launch

  • Started in 2009
  • Bunch of Googlers worked on it.

Go Facts

  • Go is used by Google
  • Maintained by Google
  • Open Source - GPT
  • Go’s compiler is written in Go

Go is picky

  • If its worth a warning its worth an error
    • Unused Variables
    • Unused imports
    • Some “style violations”
      if stuff
      {
        // This is an error
      }
      

Idioms in Go

  • Common patterns that Go developers recognize
  • Examples
    • Getters don’t need to start with Get
    • Don’t insert superflous semicolons
    • Use a switch statement over a long if-else chain
    • The “comma ok” idiom
  • Idiomatic go code uses common patterns to solve problems

Style in Go

  • No flexibility at all
  • Examples:
    • Tabs not spaces…
    • Curly braces on same line…
  • gofmt will fix style

Benefits

  • Easy to read
  • Easy to write
  • Garbage collected
  • Concurrent programming features

Hello World

package main
import(
  "fmt"
)
function main(){
  fmt.Println("hello world")
}

More Go facts

  • Go is compiled
    • You can run small programs with go run
    • go build will allow you to build larger projects.
    • go install will fully install the project
    • The Go compiler has a bunch of commands:
      • go fmt - format your code
      • go doc - displays package documentation
  • Go is statically typed
    int x = 0
    // x is always an int
    
  • Comment lines with // and /* */
  • fmt.Println() is one way to write to stdout
    • has to be imported

Variables

  • You can declare variables many ways:
    • var x int
    • var x, y, z int
    • var x, y, z int = 1, 2, 3
    • Implicit types:
      • x := 10 //int
      • x, y, z := 1, 2, 3
      • a, b, c := "a", 10, false

    Types

    • Go is statically typed
    • You can specify types ahead of time
    • Sometimes, Go can figure it out
    • Built-in types:
      • Boolean type
        • bool
          • Predeclared constants: true, false
      • Numeric types
        • unsigned integer types
          • uint, uint8, uint16, uint32, uint64
            • uint is either 32 bits or 64 bits based on compiler implementation
        • signed integer types
          • int, int8, int16, int32, int64
            • int is either 32 bits or 64 bits based on compiler implementiation
        • IEEE-754 floating point types
          • float32, float64
            • float64 is default
        • Complex number types
          • complex64, complex128
            • complex128 is default
        • Byte
          • byte is an alias for uint8
            • not converted to/from uint8 automatically
        • Rune
          • rune is an alias for uint32
            • No automatic conversion
          • Represents a Unicode code point (like a char)
      • String type
        • string
          • Possibly empty sequence of bytes
          • Immutable
          • You can check length with len(<string>)
      • Array type
        • [<const number of elements>]<element type>
        • Indexed starting at 0
        • Check length with len(<array>)
      • Slice
        • []<element type>
        • “… a descriptor for a contiguous segment of an underlying array…”
        • Backed by an array of some given capacity
        • Also indexed at 0
        • You can check length with len(<slice>)
        • Capacity can be checked with cap(<slice>)
        • Basically functions like an arraylist
        • PRIME TEST MATERIAL
      • Absence of value
        • nil

        Examples

        Number types

        i := 42 // int
        j := 50.5 // float64
        k := 0.5 + 10i //complex128
              
        var i int64 = 42 // int64
        

        Array

        var myArray [10]int
        myArray[1] = 1
        fmt.Println(myArray) // [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
        

        Slice

        mySlice := []int {10, 12, 14, 16}
        len(mySlice) // 4
        fmt.Println(mySlice) // [10, 12, 14, 16]
        
        myotherSlice := []int {}
        var mythirdSlice []int // fundamentally different, doesn't create backing array
        

Foreshadowing

  • Variable-related topic
    • Pointers
    • Memory allocation
  • More types
    • Struct type
    • Map type
    • Channels - Used in communication
    • Interface types
  • Miscellaneous items
    • Uninitialised variables are given a zero-value by default
      • 0 for numeric types
      • false for boolean type
      • ”“(empty) for string
  • The compiler will enforce proper use of types
    x := 12 // int
    x = 10 // Fine
    x = "frog" // NOPE
    x = 12.5 // NOPE
    
  • The compiler will not implicitly convert types. You have to do it yourself.
    i := 42
    y := 5.5
    fmt.Println(i+y) // NOPE
    fmt.Println(float64(i)+y) // Fine
    
  • The blank identifier ( _ ) is used for ignoring unwanted values
    x, _ := functhatreturnstwoitems()