chapter two

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, and 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 have a hearty welcoming message!

Since 1972, learning programmers discover 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 Example

2.1.3 Calling the greet function

2.1.4 Testing a specific function with testing

2.2 Are you a polyglot?

2.2.1 Parlez-vous français? The switch angle

2.2.2 Adapt the test with Test<Function> functions

2.3 Supporting more languages with a phrasebook

2.3.1 Introducing the Go map

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 (CLI)

2.5 Exercises

2.6 Summary