control_flow/

directory
v0.0.0-...-9fa2476 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2025 License: Apache-2.0

README

Control Flow

Control Flow in Go is similar to most C family languages but has some novel differences. Classic structures like if and switch are present but their syntax is a little different from what you might be used to.

There are no ternary operators.

Notes

  • if statements require an exact boolean expression. There is no "truthy" or "falsey".
  • if statements may have a pre-expression similar to the first part of a for statement.
  • switch statements do not require breaks and will not fall through by default.
  • switch statements can have multiple values per case.
  • Use the || "or" and && "and" operators for complex conditions.
  • Use shorter variable names that still provide context for the value they represent.

Code Review

if statements (Go Playground)
switch statements (Go Playground)
variable names (Go Playground)

Exercises

Exercise 1

Write a program that inspects a user's name and greets them in a certain way if they are on a list or in a different way if they are not. Also look at the user's age and tell them some special secret if they are old enough to know it.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how to use if statements.
Sample program to show how to use if statements.
Sample program to show how to use switch statements.
Sample program to show how to use switch statements.
exercises
exercise1 command
Write a program that inspects a user's name and greets them in a certain way if they are on a list or in a different way if they are not.
Write a program that inspects a user's name and greets them in a certain way if they are on a list or in a different way if they are not.
template1 command
Write a program that inspects a user's name and greets them in a certain way if they are on a list or in a different way if they are not.
Write a program that inspects a user's name and greets them in a certain way if they are on a list or in a different way if they are not.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL