2

I'm trying to execute some code from the following book:
Learning Go - An Idiomatic Approach to Real-World Go Programming written by Jon Bodner

The first chapter talks about making a Makefile to automate executing certain commands, like the go fmt some_code.go command.
The book says that writing the command like this: go fmt ./... will cause the command to run over your entire project.

However when I try to run this command I get the following error:

pattern ./...: directory prefix . does not contain main module or its selected dependencies

When I try to look up this ./... argument online, I can't find anything.

So I was wondering what this argument does in the command line.

By the way I'm using a zsh terminal on a MacOS system.

So if anybody knows what this ./... means, what I am doing wrong or what I am supposed to be doing, that would be much appreciated.

Thanks

pilu
  • 21
  • 4
  • https://www.hacksparrow.com/nodejs/directory-references.html is written for node.js, but the principle applies here as well. `The usage of ./ and ../ come from operating system relative directory dot notations. ./ refers to the present working directory (a.k.a current working directory), ../ refers to the parent of the present working directory.` – Barry Carter Jul 17 '22 at 14:51
  • FWIW, I ran into this issue because I did not have a go.mod file. I ran: `go mod init ` and no longer ran into the issue mentioned in the OP. – Chasen Bettinger Dec 29 '22 at 04:03

1 Answers1

3

The package specification ./... refers to packages in the current file system directory and all subdirectories.

The error from your go fmt command invocation indicates that the go tool could not find any packages in the directory. Perhaps you need to create a go.mod file.

The go command documentation describes the ./... feature. Here are the relevant quotes:

The section on relative import paths says:

An import path beginning with ./ or ../ is called a relative path. The toolchain supports relative import paths as a shortcut in two ways.

First, a relative path can be used as a shorthand on the command line. If you are working in the directory containing the code imported as "unicode" and want to run the tests for "unicode/utf8", you can type "go test ./utf8" instead of needing to specify the full path. Similarly, in the reverse situation, "go test .." will test "unicode" from the "unicode/utf8" directory. Relative patterns are also allowed, like "go test ./..." to test all subdirectories. See 'go help packages' for details on the pattern syntax.

The section on package patterns says:

An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.