Skip to content

Instantly share code, notes, and snippets.

@ngauthier

ngauthier/README.md

Last active May 21, 2016
Embed
What would you like to do?
Gobridge Boston Day 1

Gobridge Boston Day 1

Hello and welcome to gobridge's day 1 content! The goal of this page is to guide you through the basics of programming in Go using the site Go by Example.

Please follow along with the class as we do each lesson, try not to skip ahead, and focus on the lesson we're on. If you finish early, play around by changing the code we're working with for the current lesson, or help a friend!

Lesson 1: Hello World

  1. Open https://gobyexample.com and click on the first link, "Hello World"

On each example page, there's a description on the left and the code on the right.

Any code block can be run right in your browser by clicking the tiny gopher in the top right of the code block. This is the easiest way to get started with Go, since we don't have to install anything to your computer!

  1. Click the tiny gopher in the top right

You are now on the Go Playground web site. The Go Playground lets you run Go code really easily, right in your browser.

  1. Click "Run"

When you run the code, it gets run on the Go Playground's servers, and the output of the program is printed in the bottom section of the screen. You'll see in the bottom it said "hello world" then in a gray font "Program exited.". The "hello world" came from our program, and the "Program exited." is from the Go Playground letting us know that the program finished running.

  1. In the code, change "hello world" to say hello to yourself!

My name is Nick so I'll make it say "hello Nick". Then click Run and it should say hello to you. Awesome! You just developed some code!

Developers refer to this as the "dev loop" or "development loop". It means writing some code then running it then seeing how it does then deciding what to do next. You keep on looping through those actions until your code works.

  1. Select and copy the line of code that is printing out "hello world" and past it a few times, and have it write out a story.

  2. There's a special character called a "newline" character that instead of printing a letter or number to the screen it starts a new line. The newline is usually written as "\n". Try adding "\n" to a message.

For example, you could write:

fmt.Println("hello\nworld")
  1. Rewrite your code so that you only have one print line of code, but when your program is run it writes your story to the screen with multiple lines of text.

Discuss: As a group, what do we think the rest of the parts of code mean?

p.s. if you're comfortable, feel free to install go to your local computer and use an editor you'd like, but don't spend too much time on it :)

Lesson 2: Values

  1. Go back to https://gobyexample.com and click on the second link, "Values"

  2. Read through the description on the left and the code on the right.

  3. Consider the following list of things. What type of value would you use for each one?

  1. The price of a sandwich
  2. The color of a car
  3. The number of people in a class
  4. If it's raining outside
  1. For each type "string", "integer", "float", "boolean" come up with your own example and share it with your neighbor.

  2. Click the tiny gopher to open the example in the Go Playground

  3. Get your program to print out the following sentence:

    Hi, I'm NAME and I am AGE years old. My favorite color is COLOR and I have $X.XX in my pocket!

Each time there's something in CAPITALS, use a comma and use the right Go type for that thing. For example:

fmt.Println("My favorite color is ", "blue")

Discuss: What kinds of things could you not represent with these four types?

Lesson 3: Variables

  1. Open the "Variables" example on Go by Example

  2. Read through the description and the code example

  3. Out of all these ways to define variables, which is your favorite, and why?

  4. Open the code up on the Go Playground

  5. Write the sentence from the last exercise, but this time, use a variable for each CAPITAL

  6. What happens if you define a variable with the wrong type? Try it out!

  7. What happens if you try to set the same variable to a different type? Try it out!

For example:

f := "short"
f := 5

Discuss: Why would it be useful to define a variable?

Lesson 4: Constants

  1. Open the "Constants" example and read through it

  2. What do you think it means for something to be constant? How is this different from a variable?

  3. Open the example on the Go Playground and test your hypothesis!

Discuss: What did you find out?

Lesson 5: For

  1. Open the "For" example and read through it

  2. What does the condition for the For loop do?

  3. What is each part of the "classic" loop?

  4. Why would you ever use a loop that never stops?

  5. Open the code in the Go Playground

  6. Write a program that counts to 10, printing out the numbers 1 through 10

  7. Write a program that counts forever! What happens?

Discuss: What are some examples of things in the real world that loop?

Lesson 6: If/Else

  1. Open the "If/Else" example and read through it

  2. What does "%" do?

  3. What does "==", "<", and ">" do? What type is the result of using those operators? What type do if statements us?

  4. Combine a For loop with an If/Else to count to 10 and print out if a number is even or odd.

The program output should look like:

1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even

Discuss: What bugs did you have while you were working on the code? What problems did you run into?

Bonus: what does the Format button do?

Lesson 7: Switch

  1. Open the "Switch" example and read through it

  2. What is the difference between using Switch and using a bunch of If/Else?

  3. Rewrite your program from Lesson 6, this time using a For loop with a Switch statement for even and odd

  4. Show your solution to your neighbor, discuss the differences

Discuss: Why would you use Switch instead of If/Else?

Lesson 8: Arrays

  1. Open the "Arrays" example and read through it

  2. Write a program that does the following:

  1. Creates an array of size 10 and prints it out -> [0 0 0 0 0 0 0 0 0 0]
  2. Loops through the numbers 1 through 10, and sets the value in each slot of the array to the number in the loop
  3. Print out the array [1 2 3 4 5 6 7 8 9 10]
  4. Creates a boolean array of size 10
  5. Loop through the first array, and set the second array's value to whether the number in the first array is even
  6. Print out the second array -> [false true false true false true false true false true]
  7. Give someone a high five, that was the hardest program so far!

Discuss: What errors did you run into? What was the solution?

Lesson 9: Slices

  1. Open the "Slices" example and read through it

  2. How are slices different from arrays?

  3. Make a program that does the following:

  1. Creates an empty slice of integers and prints it out -> []
  2. Loops over the numbers 1 through 10 and appends them to the slice
  3. Prints out the slice -> [1 2 3 4 5 6 7 8 9 10]
  4. Prints out the first 5 numbers in the slice -> [1 2 3 4 5]
  1. Share your code with your neighbor, what was different?

Discuss: What problems did you run into?

Lesson 10: Maps

  1. Open the "Maps" example and read through it

  2. How are maps like slices, and how are they different?

  3. Make a program that does the following:

  1. Creates a map from integers to booleans
  2. Loops through the numbers 1 through 10, and sets the map where the key is the number and the value is whether the number is even (HINT: use an If/Else or Switch!)
  3. Print out the map -> map[1:false 2:true 3:false 4:true 5:false 6:true 7:false 8:true 9:false 10:true]
  4. Print out whether 4 is even using variables -> The number 4 is even: true
  1. Share your code with your neighbor: what was different?

Discuss: What problems did you run into?

Lesson 11: Range

  1. Open the "Range" example and read through it

  2. Modify your program from lesson 10 to use a range to iterate the map to output the following:

    1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even

HINT: use a for loop with range to get the key and value of each entry in the map, then use an If/Else on the value.

Discuss: When is range useful in a loop instead of the initial/condition/after classic loop style?

Lesson 12: Functions

  1. Open the "Functions" example and read through it

  2. Write a program that does the following:

  1. Loop through the numbers 1 through 10
  2. Call a function that prints out if the number is even or odd (like in Lesson 11's output).
  1. Refactor your program to add a new function that takes an integer and returns a boolean signifying if the integer is even, use that function inside your printing function

  2. Share your code with your neighbor, what was different?

Discuss: Why are functions useful?

Bonus: add a third function. Extra bonus points if you have a function in a function in a function!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment