User input and output
According to the Unix philosophy, when a program finishes its job successfully, it generates no output. However, for a number of reasons, not all programs finish successfully and they need to inform the user about their issues by printing appropriate messages. Additionally, some system tools need to get input from the user in order to decide how to handle a situation that might come up.
The hero of Go user input and output is the fmt
package, and this section is going to show you how to perform these two tasks by starting with the simplest one.
Note
The best place to learn more about the fmt
package is its documentation page, which can be found at https://golang.org/pkg/fmt/.
Getting user input
Apart from using command-line arguments to get user input, which is the preferred approach in systems programming, there exist ways to ask the user for input.
Two such examples are the rm(1)
and mv(1)
commands when used with the -i
option:
$ touch aFile$ rm -i aFileremove aFile? y$...