7

I am trying to write a Makefile command that will output an error if the Go code is not correctly formatted. This is for a CI step. I am struggling with how to get it working in the make file. This solution works on the bash command line:

! gofmt -l . 2>&1 | read

But copying this into the Makefile:

ci-format:
    @echo "$(OK_COLOR)==> Checking formatting$(NO_COLOR)"
    @go fmt ./...
    @! gofmt -l . 2>&1 | read

I get the following error:

/bin/sh: 1: read: arg count
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
snorberhuis
  • 3,108
  • 2
  • 22
  • 29

1 Answers1

15

These days, I use golangci-lint, which includes gofmt checking as an option.

But if for some reason you want to do this yourself, the command I previously used for precisely that purpose is:

diff -u <(echo -n) <(gofmt -d ./)

See, for example, the .travis.yml files on one of my projects.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189