Updating the Phone Book Application I

Let’s follow step-by-step instructions on using environment variables to enhance our phone book application.

The functionality that is added to this new version of the phone book utility is the following:

  • The CSV file path can be optionally given as an environment variable named PHONEBOOK.

  • The list command sorts the output based on the surname field.

Although we could have given the path of the CSV file as a command-line argument instead of the value of an environment variable, it would have complicated the code, especially if that argument was made optional. More advanced Go packages such as viper simplify the process of parsing command-line arguments with the use of command-line options such as -f followed by a file path or --filepath.

Lastly, if the PHONEBOOK environment variable is not set, then the utility uses a default value for the CSV file path. Generally speaking, not having to recompile our software for user-defined data is considered a good practice.

Note: The current default value of CSVFILE is usually set to the machine’s home directory—we should change that default value to fit our needs or use a proper value for the PHONEBOOK environment variable.

Setting up the value of the CSV file

The value of the CSV file is set in the setCSVFILE() function, which is defined as follows:

Get hands-on with 1200+ tech skills courses.