“./”在终端命令中表示当前目录。

huangapple go评论69阅读模式
英文:

What does the following argument in a terminal command mean: ./

问题

我正在尝试执行以下书籍中的一些代码:
《Learning Go - An Idiomatic Approach to Real-World Go Programming》(Jon Bodner著)

第一章讲述了如何创建一个Makefile来自动执行某些命令,比如go fmt some_code.go命令。
书中说,将命令写成go fmt ./...会导致该命令在整个项目上运行。

然而,当我尝试运行这个命令时,我得到了以下错误:

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

当我尝试在网上查找./...这个参数时,找不到任何相关信息。

所以我想知道这个命令行参数的作用是什么。

顺便说一下,我在MacOS系统上使用的是zsh终端。

如果有人知道./...是什么意思,我做错了什么或者我应该做什么,那将不胜感激。

谢谢!

英文:

I'm trying to execute some code from the following book:<br/>
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.<br/>
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:<br/>

> 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

答案1

得分: 3

包规范./...指的是当前文件系统目录及其所有子目录中的包。

你的go fmt命令调用出错表明go工具无法在该目录中找到任何包。也许你需要创建一个go.mod文件。

go命令文档描述了./...的功能。以下是相关的引用:

关于相对导入路径的部分说:

> 以./或../开头的导入路径称为相对路径。工具链支持相对导入路径作为两种快捷方式。

> 首先,相对路径可以在命令行上使用作为快捷方式。如果你正在工作的目录中包含被导入为"unicode"的代码,并且想要运行"unicode/utf8"的测试,你可以输入"go test ./utf8",而不需要指定完整路径。类似地,在相反的情况下,"go test .."将从"unicode/utf8"目录测试"unicode"。相对模式也是允许的,比如"go test ./..."来测试所有子目录。有关模式语法的详细信息,请参阅"go help packages"。

关于包模式的部分说:

> 如果导入路径包含一个或多个"..."通配符,则它是一个模式,每个通配符可以匹配任何字符串,包括空字符串和包含斜杠的字符串。这样的模式会扩展为在GOPATH树中找到的与模式匹配的所有包目录。

英文:

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.

huangapple
  • 本文由 发表于 2022年7月17日 22:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/73012731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定