CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Introduction to Go


  1. Properties of Programming Languages
  2. Running Go
  3. Using the cs112 Go Library
  4. Hello World in Go
  5. Basic Console Output
  6. Constants and Operations
  7. Declaring Variables
  8. Function Basics
  9. Conditionals
  10. Testing

  1. Properties of Programming Languages
    • Compiled vs Interpreted:
      • In an interpreted language like Python, an "interpreter" program reads your code and executes it one line at a time. Interpreted languages are relatively slow, but more portable.
      • In a compiled language, a "compiler" program reads your code and transforms it directly into machine code that your computer can run. Compiled languages are fast, but less portable.
    • Statically vs Dynamically Typed:
      • In a dynamically typed language like Python, variables are not declared before their use, type checking happens at runtime, and variables can change type.
      • In a statically typed language, variables are declared before use, type checking happens at at compile time, and variables cannot change type.
    • Go is a compiled, statically typed language

  2. Running Go
    Note: Your TA's will be happy to help with any of these steps!
    • Install Go 1.14.4 from golang.org's download page.
    • Configure VSCode (see the Getting Started with VSCode notes here)
    • Run VSCode (the default Go text editor / IDE)
    • Edit your Go file
    • Run your code (command-G or control-G in VSCode once you followed our setup instructions)

  3. Using the cs112 Go Library
    Note: Your TA's will be happy to help with any of these steps!
    • Run the command go get github.com/CMU15-112/golang in your VSCode terminal.
    • Along with "fmt", import "github.com/CMU15-112/golang" into your file.

  4. Hello World in Go
    /* All Go programs are made of packages. package main tells us that this file is our program's entrypoint. (By the way, this is a multiline comment)*/ package main // We import the fmt library to print to the console and handle strings // (And this is a single line comment) import "fmt" // When package main runs, its main function is automatically called // Functions in Go are defined with "func" instead of Python's "def" func main() { /* Rather than using whitespace to indicate that we're inside a function conditional or loop, Go uses curly braces */ // And finally we print! fmt.Println("Hello World!") }

  5. Basic Console Output
    • Printing in the same line
      package main import "fmt" func main() { // You can separate multiple values with commas fmt.Println("Don't forget", "to be awesome") // Println automatically adds a newline. Print doesn't fmt.Print("Don't forget ") fmt.Println("to be awesome") }

    • String formatting
      package main import "fmt" func main() { x := 42 y := 99 // %d is used for digits, %c is used for chars, and %s is used for strings fmt.Println(fmt.Sprintf("Did you know that %d + %d is %d?", x, y, x+y)) fmt.Println(fmt.Sprintf("Carpe %s", "Diem")) }

  6. Constants and Operations
    • Booleans and Comparison
      package main import "fmt" func main() { fmt.Println(true) fmt.Println(true || false) // || means or fmt.Println(true && false) // && means and fmt.Println(2 > 3) fmt.Println(5 <= 10) }

    • Math operations
      package main import "fmt" func main() { fmt.Println(42 + 112) fmt.Println(10 / 3) // Dividing integers produces integer results fmt.Println(10.0 / 3) // Dividing floats produces float results fmt.Println(10 % 3) fmt.Println(10 * 2 + 9 / 3) }

    • The math module
      package main import ( "fmt" "math" // Here we import the math module ) func main() { fmt.Println(math.Pow(2, 3)) fmt.Println(math.Pow(2, -1)) fmt.Println(math.Hypot(3, 4)) // The distance formula }

  7. Declaring Variables
    • Explicit types with var
      package main import "fmt" func main() { // Here we define the variable n as an integer and print it var n int n = 5 fmt.Println(n) }

    • Go is statically typed
      package main import "fmt" func main() { /* Attempting to set a variable equal to a value before the variable is declared causes a "compile time" error. These errors happen right after syntax errors, before the code can run. */ fmt.Println("Will I run?") n = 5 fmt.Println(n) }

      package main import "fmt" func main() { // Setting an integer n to a string causes a compile time error as well fmt.Println("Will I run?") var n int n = "oops" fmt.Println(n) }

    • Implicit typing with :=
      package main import "fmt" func main() { // Here we set the type and value of the variable n simultaneously n := 5 fmt.Println(n) }

  8. Function Basics
    • Defining Functions
      package main import "fmt" /* Function parameters must be explicitly typed. The variable name comes first, then the type. The return value is also typed.*/ // timesTwo takes in an int n and returns an int func timesTwo(n int) int { return 2 * n } // Multiple parameters can share a type func multiply(n, m int) int { return n * m } // Or parameters can have different types. This function has no return type func printOnCondition(b bool, s string) { if b { fmt.Println(s) } } func main() { fmt.Println(timesTwo(5)) fmt.Println(multiply(42, 2)) printOnCondition(6 > 5, "Six is bigger!") }

    • Returning Multiple Values
      package main import "fmt" // Functions can return multiple values, similar to Python func doubleAndAddB(n int, s string) (int, string) { return 2 * n, s + "B" } func main() { n, s := doubleAndAddB(5, "A") fmt.Println(n, s) }

  9. Conditionals
    • If / Else If / Else
      package main import "fmt" // In Go, "elif" is called "else if" func compare(n, m int) { if n < m { fmt.Println(n, "is smaller than", m) } else if n > m { fmt.Println(n, "is bigger than", m) } else { fmt.Println(n, "is equal to", m) } } func main() { compare(5, 6) compare(10, 2) compare(3, 3) }

    • Style is Syntax
      package main import "fmt" /* "else" and "else if" MUST go on the same line as the closing bracket from the previous statement. This code will not compile. */ func compare(n, m int) { if n < m { fmt.Println(n, "is smaller than", m) } else if n > m { fmt.Println(n, "is bigger than", m) } else { fmt.Println(n, "is equal to", m) } } func main() { compare(5, 6) compare(10, 2) compare(3, 3) }
  10. Testing
    package main import ( "fmt" "github.com/CMU15-112/golang" ) func multiply(x, y int) int { return x * y } func testMultiply() { fmt.Print("Testing multiply...") cs112.Assert(multiply(2, 3) == 6) cs112.Assert(multiply(2, 0) == 0) cs112.Assert(multiply(5, -1) == -5) cs112.Assert(cs112.Equals(multiply(56, 2), 112)) fmt.Println("Passed!") } func main() { testMultiply() }