These are answers to the questions shown at golangr.com. They are part of the Go (golang) programming tutorial.
Its recommended to try the exercises before, instead of overlooking the answers. That is because programming is learned by trial and error, practice.
Hello world
Create a program that shows your name1
2
3
4
5
6
7package main
import "fmt"
func main() {
fmt.Println("Bob")
}
Create a program that shows your address1
2
3
4
5
6
7package main
import "fmt"
func main() {
fmt.Println("Street 123, City X, Country Y")
}
Comments
Create a program and add a comment with your name
1 | package main |
Strings
Create a program with multiple string variables
1 | package main |
Create a program that holds your name in a string.
1 | package main |
Keyboard input
Make a program that lets the user input a name
1 | package main |
Get a number from the console and check if it’s between 1 and 10.
1 | package main |
Variables
Calculate the year given the date of birth and age
Create a program that calculates the average weight of 5 people.
1 | package main |
Scope
What’s the difference between a local and global variable?
Where the variable can be used in the code.
A global variable can be used in the entire code file.
A local variable only in a function, loop or section.
How can you make a global variable?
Define it on top of the file
Arrays
Create an array with the number 0 to 10
1 | package main |
Create an array of strings with names
1 | package main |
For loops
Can for loops exist inside for loops?
Yes they can. That’s called nested loops.
Make a program that counts from 1 to 10.
1 | package main |
Range
What is the purpose of range ?
To iterate over all the elements of a data structure, array, slice and others.
What the difference between the line for index, element := range a and the line for _, element := range a ?
index holds the position of the element in the data structure. Sometimes you may not need it, then you can use underscore.
If statements
Make a program that divides x by 2 if it’s greater than 0
1 | package main |
Find out if if-statements can be used inside if-statements.
Yes, that’s called nested-if.
While loops
How does a while loop differ from a for loop?
A for loop has a predefined amount of iterations. A while loop doesn’t necessarily.
Files
Read file
Think of when you’d read a file ‘line by line’ vs ‘at once’?
If you have a very large file, line by line is better because otherwise it won’t fit into memory.
Create a new file containing names and read it into an array
See article
Write file
Write a list of cities to a new file.
1 | package main |
Rename file
Which package has the rename function?
os
Struct
How does a struct differ from a class?
Classes are a concept of object oriented programming.
A class can be used to create objects. Classes can inherit from another.
Maps
What is a map?
A Golang map is a unordered collection of key-value pairs.
Is a map ordered?
No
What can you use a map for?
To store key value pairs
Random numbers
Make a program that rolls a dice (1 to 6)
1 | package main |
Can you generate negative numbers?
1 | randomNum := random(-7, 7) |
Pointers
Where are variables stored in the computer?
Memory
What is a pointer?
A pointer is a variable whose address is the direct memory
address of another variable.
How can you declare a pointer?
Use the asterisk , `var var_name var-type`.
Slices
Take the string ‘hello world’ and slice it in two.
1 | package main |
Can you take a slice of a slice?
Yes
Methods
Create a method that sums two numbers
1 | package main |
Create a method that calls another method.
See above main() calls sum().
Multiple return
Can a combination of strings and numbers be used?
Yes
Variadic functions
Create a variadic function that prints the names of students
1 | package main |
Recursion
When is a function recursive?
A function is recursive if it:
Calls itself
Reaches the stop condition
Can a recursive function call non-recursive functions?
Yes, it can
Goroutines
What is a goroutine?
A goroutine is a function that can run concurrently.
How can you turn a function into a goroutine?
Write go before the function call
Concurrency
What is concurrency?
Concurrency, do lots of things at once. That’s different from doing lots of things at the same time
Channels
When do you need channels?
To communicate between goroutines
How can you send data into a channel?
1 | c <- "message" |
How can you read data from a channel?
1 | msg := <- c |