英文:
Use Absolute path instead for go "fmt" in import directive
问题
这是一个教学性的问题,而不是一个程序性的问题,因为只需要简单地使用"fmt"就可以正常工作,但是当我修改hello world的golang文件如下所示时:
package main
import "golang.org/fmt"
func main() {
fmt.Println("Hello, world")
}
我得到的回应是:
go:3:8: no required module provides package golang.org/fmt; to add it:
go get golang.org/fmt
我可以在/usr/local/go/src/fmt中看到fmt包,并且它与https://golang.org/src/fmt/中的文件相同。
在上述文件中,我可能非常接近正确的绝对路径,可以包含fmt。谢谢!
英文:
This is an instructional question and not a procedural one as simply requiring "fmt" works just fine, but when with the hello world golang file I modify it as follows
package main
import "golang.org/fmt"
func main() {
fmt.Println("Hello, world")
}
I get in response:
go:3:8: no required module provides package golang.org/fmt; to add it:
go get golang.org/fmt
I can see the fmt package in /usr/local/go/src/fmt and it mirrors the files in https://golang.org/src/fmt/
I am probably very close in the above file, what is the correct absolute path that would work to include fmt ? Thank you!
答案1
得分: 3
包的正确绝对导入路径是fmt。
相对导入路径以./或../开头。导入路径fmt是绝对导入路径。
远程导入路径以域名开头。该包没有远程导入路径。
工具链为每个导入路径创建一个唯一的包。如果应用程序可以使用远程导入路径引用fmt包的源代码,则具有远程路径的包将与标准的fmt包不同。包的每个方面都是唯一的。代码是重复的。每个包都有一个ScanState类型,这些类型不能互换使用。pp缓存也是重复的。等等。
英文:
The correct absolute import path for the package is fmt.
Relative import paths start with ./ or ../. The import path fmt is an absolute import path.
Remote import paths start with a domain name. The package does not have a remote import path.
The tool chain creates a unique package for each import path. If the application could refer to the source code for the fmt package using a remote import path, the package with the remote path will be different from the standard fmt package. Every aspect of the package is unique. The code is duplicated. There is a ScanState type for each package and these types cannot be used interchangeably.
The pp cache is duplicated. And so on.
答案2
得分: 0
在这种情况下,fmt是完全限定的路径。将fmt文档[1]与golang.org/x/text文档[2]进行比较。
Go标准库确实有一个go.mod[3],但尝试导入std/fmt也不起作用。
- https://pkg.go.dev/fmt
- https://pkg.go.dev/golang.org/x/text
- https://github.com/golang/go/blob/master/src/go.mod
英文:
In this case, fmt is the fully qualified path. Compare the fmt docs [1] with golang.org/x/text docs [2].
Go standard library does have a go.mod [3], but trying to import std/fmt doesn't work either.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论