

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Formatting
incomplete
2: Check Formatting
incomplete
3: Formatting CI
incomplete
This lesson's interactive features are locked, please to keep using them
Unfortunately (in my opinion) the go fmt command always exits with status code 0. Luckily go fmt prints the names of all the files it fixes, so if we want to fail a CI check when a repo isn't formatted, the easiest way is to make sure that nothing is printed to stdout.
We can use the test command to do so:
test -z $(go fmt ./...)
Let's break down how it works:
go fmt ./...: Runs the go fmt tool on the current directory and all its subdirectories (that's what ./... stands for). go fmt returns the names of files that it has formatted. If no files need formatting, it will return an empty output.$(go fmt ./...): The $() syntax is used for command substitution in bash. It runs the command inside the parentheses, and then replaces the $() in the command line with the output of that command.test -z $(go fmt ./...): The test command is built into bash. The -z option checks if the following argument is an empty string, returning 0 if it is, and 1 if it isn't.test -z $(go fmt ./...)
echo $?
The echo $? command prints the exit code of the last command that was run. You should see that it prints 1, indicating that the repo is not formatted properly.
However, it should have also fixed the formatting!
test -z $(go fmt ./...)
echo $?
You should see that it prints 0, indicating that the repo is formatted properly.
Run and submit the CLI tests.