Lesson 1 · Unit 01
Values and expressions
The frame of a Go program, printing, numbers, text and arithmetic.
What you practise
package mainandfunc main,fmt.Println, numbers and text, arithmetic and its order, whole-number division and%
Try a question from this lesson
Pick one
This is a whole Go program. The two lines in the middle each show something on the screen. What do you think appears?
package main
import "fmt"
func main() {
fmt.Println("Hello")
fmt.Println("Bitpath")
}Correct answer
Hello, then Bitpath under it
Why
A program runs its lines from top to bottom, one at a time. fmt.Println prints what is inside its brackets and then moves to a new line. So Hello comes first, and Bitpath lands under it.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The shape of a Go program
Every Go program has the same frame. package main says "this is a program you can run". import "fmt" borrows the tools for printing. func main() { ... } holds your instructions: Go runs the lines between the braces, top to bottom.
Before it runs anything, Go compiles the file: it reads all of it, and refuses to start if a line breaks the rules.
package main
import "fmt"
func main() {
fmt.Println("I run first")
fmt.Println("I run second")
}Concept
Text, numbers and notes
Text in double quotes is called a string. Go prints it as it is. Numbers are written without quotes, and Go can calculate with them: +, -, * for times and / for divide.
Anything after // on a line is a comment: a note for people, which Go ignores.
To save space, lessons from here on show only the lines inside func main. The frame around them is still there.
fmt.Println("7 * 6") // a string
fmt.Println(7 * 6) // a number: 42
fmt.Println(10 - 4) // 6Concept
Whole-number division and remainders
Go keeps two kinds of number apart: whole numbers like 7, and numbers with a decimal point like 7.0.
Dividing two whole numbers gives a whole number: 7 / 2 is 3. The sign % gives what is left over, the remainder: 7 % 2 is 1. Write a decimal point when you want the fraction: 7.0 / 2 is 3.5.
fmt.Println can print several values at once, with commas between them. It puts a space between each.
fmt.Println(7 / 2) // 3
fmt.Println(7 % 2) // 1
fmt.Println(7.0 / 2) // 3.5
fmt.Println(7/2, 7%2) // 3 19 more exercises in the app
- 1× order the lines
- 3× multiple choice
- 3× type the answer
- 1× fill the blanks
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.