Homework #1

Assigned: August 27
Due: September 3, 12:30pm


(60 points) Implement a primitive version of the Linux file comparison program diff in Go.


  1. Your program must be written in Go and compile without errors or warnings on our SUSE Linux system.
  2. Do not prompt for input.
  3. The two input files are given on the command line using file I/O. For instance,
    $ ./diff1 file1 file2
  4. Two files are identical if they match exactly character by character.
  5. If the two input files are identical, print nothing to standard output and exit with a 0 exit status.
  6. If the two input files are different, print the line numbers (the first line of each file is line 1) on which they differ one per line. For instance,
    $ ./diff1 file1 file2
  7. A file name of - stands for text read from the standard input.
  8. As a special case, diff - - compares a copy of standard input to itself. Do not copy os.Stdin to a file and then diff on that file.
  9. Normal program output must only be written to standard output.
  10. Abnormal program output (e.g., error messages) must only be written to standard error.
  11. Support the following command-line options:
    1. -l: ignore leading whitespace in the comparison, where whitespace is any contiguous series of tabs or spaces.
    2. -t: ignore trailing whitespace in the comparison.
    3. -m: ignore intermediary whitespace in the comparison (i.e., whitespace neither at the beginning or the end of each line).
    4. -a: ignore all whitespace in the comparison.

  12. All options must precede both input filenames.
  13. Options can be given individually and in any order (e.g., -l -t or -t -l).
  14. If no options are given, the comparison is exact.
  15. If an invalid option or filename is given, your program must print the same error message diff would print to standard error in that particular situation and halt with the same non-zero exit status.
  16. If any other option, valid or otherwise, is given with the -a option, your program must print the following error message to standard error and halt with exit status 9:
    $ ./diff1 -t -a file1 file2
    Option -a cannot be combined with any other options.
  17. Prepare your source code file as /home/<logname>/homeworks/hw1/diff1.go.


If designed properly, the program required to solve this homework problem should occupy no more than 200 lines of code. You may find this webpage on command-line processing in Go helpful. Also have a look at the following Go packages for a helpful functions to use: bufio, fmt, strings, flag, os, log, and io.

There is also a reference executable of a solution for this homework available at /home/perugini_cps444/share/homeworks/hw1/diff1.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below (i.e., all lower case).

Prepare your source code file as /home/<logname>/homeworks/hw1/diff1.go, where <logname> is your login name (e.g., FA_15_CPS444_03). Only your source file diff1.go will be electronically collected from this location in your Linux account on the deadline.

Failure to follow these submission requirements will result in a 10% penalty.


Ninety percent of your score will come from correctness and 10% of your score will come from following our programming style guide. Applicable submission penalties will then be applied.

Return Home