Golang basics: setup, syntax, types and control flow
Install it, run something, and meet the ideas that make Go different from what you already know.
- 01Setup, your first program, and the toolchainInstall Go, run one file, and meet the four commands you will use every day.
- 02Syntax, comments, and namingThe shape every Go file takes, and the naming rules the whole ecosystem follows.
- 03Printing in Go: Println, Printf and format verbsHow to print in Go with Print, Println and Printf, and the Printf format verbs you will use every day.
- 04Basic data typesEvery built-in type, why there are so many integer sizes, and what to actually use.
- 05Constants and iotaValues fixed at compile time, and how Go does enums without an enum keyword.
- 06OperatorsArithmetic, comparison, logical, and bitwise, plus what Go leaves out on purpose.Graded challenge
- 07Type conversionWhy Go refuses to turn an int into a float for you.
- 08if, for, and switchOne loop keyword, no ternary, and cases that do not fall through.Graded challenge
What each lesson covers
By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.
1. Setup, your first program, and the toolchain
- Write and run a
package mainprogram withgo run .. - Explain what the zero value of an
int, astringand aboolis. - Predict what
go build,go fmtandgo veteach do before you run them.
Remember: Go compiles to one self-contained binary with nothing to install beside it, and every variable starts at a defined zero value. Those two facts shape most of the language.
- Write and run a
2. Syntax, comments, and naming
- Write a Go file with a package clause, an import block, and a doc-commented function.
- Explain why the opening brace must stay on the same line as
func main(). - Fix the compile errors caused by an unused import and an unused variable.
Remember: Capitalisation is not style in Go. It is the access modifier.
3. Printing in Go: Println, Printf and format verbs
- Choose between
Print,Println,PrintfandSprintffor a given output. - Predict the output of
Printfwith%v,%T,%q,%.2fand%5d. - Write a literal percent sign and a padded column with format verbs.
Remember:
%vprints anything and%Ttells you its type. Learn them before you learn a debugger.- Choose between
4. Basic data types
- Choose the right built-in type for a count, an id, a decimal and a money amount.
- Predict the output of overflowing an
int8or auint8. - Explain why
0.1 + 0.2is not exactly0.3for float64 variables.
Remember: Go gives you exact-size types for when it matters, and
intfor when it does not.5. Constants and iota
- Write a const block that numbers a set of states with
iota. - Explain why an untyped constant fits a float64 and an int64 but a typed one does not.
- Predict the values
iotaproduces in a block with a skipped first line and arithmetic.
Remember: There are no constant slices, maps or structs. Those are
var.- Write a const block that numbers a set of states with
6. Operators
- Predict the output of integer division,
%, and shifts such as3 << 10. - Write a boolean expression with
%,&&and||that answers a yes/no question. - Explain why
d != 0 && 10/d > 2never divides by zero.
Remember: None of Go’s operators can be redefined.
a + bmeans addition, or concatenation for strings, in every file you will ever read.- Predict the output of integer division,
7. Type conversion
- Write explicit conversions between
int,float64and a named type. - Explain why
string(rune(65))gives "A" whilestrconv.Itoa(65)gives "65". - Predict the output of
lenon a string with non-English characters.
Remember: Implicit numeric conversion is where money code loses cents. Go makes you write it down.
- Write explicit conversions between
8. if, for, and switch
- Write the same loop as a three-part
for, a condition-onlyforand arangeloop. - Explain why a variable declared in
if n := ...; condcannot be used after the block. - Predict which
switchcase runs, knowing that cases do not fall through.
Remember: There is no ternary operator. Write the
if.- Write the same loop as a three-part