2 Hello, earth! Extend your hello, world

 

This chapter covers

  • Writing to the standard output
  • Testing writing to the standard output
  • Writing table-driven tests
  • Using a hash table to hold key-value pairs
  • Using flags to read command-line parameters

As developers, our main task is to write valid programs. These programs are executed on a computer; they’ll accept some inputs (e.g., keys pressed on a keyboard, a signal received from a microphone) and will produce outputs (e.g., emit a beep, send data over the network). The simplest program of all does nothing, and simply exits. That wouldn’t be a very gratifying introduction to coding, would it? Instead, let’s print a hearty welcoming message!

Since 1972, learning programmers have discovered their new language through variations of the same sentence: hello, world. A programmer’s first autonomous step is, thus, usually to change this standard message and see what happens when the greeting message slightly changes. Type, compile, run, smile—this is what developing a hello, world is about.

2.1 Any travel begins at home

2.1.1 Our first program: main.go

2.1.2 Let’s test with the Example function

2.1.3 Calling the greet function

2.1.4 Testing a specific function with the testing package

2.2 Are you a polyglot?

2.2.1 Parlez-vous français? The switch angle

2.2.2 Adapt the test with Test functions

2.3 Supporting more languages with a phrasebook

2.3.1 Introducing the Go map hash table

2.3.2 Writing a table-driven test

2.4 Using the flag package to read the user’s language

2.4.1 Add a flag

2.4.2 Test the command-line interface

2.5 Side quests

Summary