Skip to content

Intro to Programming (Go)

Michelle L edited this page Mar 31, 2019 · 10 revisions

° . Table of Contents . °

  1. Data types
  2. Variables
  3. Boolean expressions
  4. If statements

[Source: DeviantArt]

Why Go?

Go (or "Golang") is an open source programming language developed by Google that was first released in 2009. Since then it has been steadily growing in adoption. We decided to use Go for the backend of this project because it's faster, more stable, and has less syntax than other popular languages.

"Go will be the server language of the future." -Tobias Lütke, founder of Shopify

Concurrency is the ability to run many processes at the same time.

Code readability vs. efficiency

Source: Keval Patel, "Why should you learn Go?" (click to read more)



Video: Ladders to the Moon (3:15 - 13:12)

Talk by Ramsey Nasser @ Eyeo 2015



1. Data types

All values in a programming language have a "type" - such as an integer, Boolean, or string - that dictates how the computer will interpret it. For example 7 + 5 is interpreted differently from "7" + "5"

Primitive data types are the basic building blocks of data. They are universal across all programming languages (for the most part).

TRY THIS:

  • Open https://goplay.space to start writing Go!
    • Ephemeral "play space"
    • Customize settings
  • Print "hello world"
  • Print 7 + 5
  • Print "7" + "5"

[View complete solution]



2. Variables

A variable is a placeholder for a piece of information that can change over time. It's actually a reference to a specific location in memory where the value is stored/updated.

  • CREATING A VARIABLE:

  • UPDATING A VARIABLE:

Instead of reading = as "equals", we read it as "gets the value"

TRY THIS:

  • Create a variable called score that gets the value 0
  • Print the value of score
  • Update score to get its current value + 5
  • Print the value of score again
  • Update score to get its current value - 2
  • Print the value of score again

[View complete solution]



3. Boolean expressions



4. If statements

Let's write a program that tells us whether a number is odd or even.

In order to do this, we'll need the modulo operator (%)

  • num % 2 returns the remainder after dividing num by 2
  • If it returns 0, we know num is an even number

Possible solutions: