Programs and algorithms in Go
A plan and a program
An algorithm is a finite sequence of steps that solves a problem. A program expresses instructions in a language a computer can execute. For a first example, imagine a shop that sells two items costing 2 and 3 units. The plan is to add the prices and display the total.
package main
import "fmt"
func main() { fmt.Println(2 + 3)}The result is 5. The + operator adds the numbers and fmt.Println displays the result on its own line. You do not need to memorize the surrounding syntax yet.
Input, process, output
For this example the input values are written directly in the code, the process is addition, and the output is 5. Real programs can also receive data from users or files. The algorithm is the idea; Go is one way of expressing it.
A common mistake is to confuse an expected result with the steps needed to produce it. Writing “5” alone does not describe how to compute the total when prices change.
Try it locally
On paper, describe the input, process and output for prices 4 and 6. Predict the result. No Go installation is needed yet.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Getting Started 01: Programs and algorithms in Go
Check the lesson objective before moving on.
Which part is the algorithm?
For prices 4 and 6, what is the result of the same process?