This gist contains the cat Unix command—written in Go.
The following Go program concatenates files passed as command-line arguments. If you don't pass any command-line arguments, then cat reads from stdin (it's the cat Unix utility).
The following procedure shows how to build and run cat.
-
Navigate to a temporary directory.
cd $(mktemp -d) -
Download cat.go Go code.
wget https://gist.github.com/mbigras/a1c96ec65deacb5fa037215849b5c2ba/raw/90bd1bc0d2db776604395b17baf6d846c1e1433e/cat.go -
Build cat Go program.
go build cat.go -
Pass some input to cat interactively from stdin.
./catType something. Press return. Press control-D. Your output should look like the following.
$ ./cat hello world # Manually type "hello world". hello world -
Pass some input through a Unix pipe—also stdin.
echo hello world | ./catYour output should look like the following.
$ echo hello world | ./cat hello world -
Pass your input through a couple files.
echo hello > foo.txt echo world > bar.txt ./cat foo.txt bar.txtYour output should look like the following.
$ echo hello >foo.txt $ echo world >bar.txt $ ./cat foo.txt bar.txt hello world -
Pass your input through a couple temporary files.
./cat <(echo hello) <(echo world)Your output should look like the following.
$ ./cat <(echo hello) <(echo world) hello world -
Pass some bad input.
./cat idontexistYour output should look like the following.
$ ./cat idontexist 2023/01/02 14:33:45 open idontexist: no such file or directory -
Clean up.
Navigate away and the temporary directory will get automatically cleaned up.