为什么在Windows上,“ gofmt -d”给出“计算差异:exec:”差异”:在%PATH%中找不到可执行文件”错误?

I would like to see what mistakes I made in my golang files. In order to figure it out, I issue a command like this:

gofmt -d myfile.go

According to the gofmt --help manual, it is supposed to list the diff of the current and the desired versions of the file. Instead of this, it produces this error message:

computing diff: exec: "diff": executable file not found in %PATH%

How could this be fixed?

The gofmt tool assumes the system has an installed and available diff. Unfortunately, this tool is not part of a standard Windows installation so you need to add it manually.

For most of us, the easiest way to add the path of the diff tool we already have on our computer. This is the one that has been installed as part of git. (If git hasn't been installed yet then do it at first.)

In order to solve this, you should issue a command from the DOS prompt in which you want to execute gofmt:

set PATH=%PATH%;c:\Program Files\Git\usr\bin

(Maybe you need to change the path according to your git installation.)

For a more permanent solution, you should add the path of diff.exe to the PATH environment variable via the Control Panel.