在导入指令中,使用绝对路径而不是”go fmt”。

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

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也不起作用。

  1. https://pkg.go.dev/fmt
  2. https://pkg.go.dev/golang.org/x/text
  3. 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.

  1. https://pkg.go.dev/fmt
  2. https://pkg.go.dev/golang.org/x/text
  3. https://github.com/golang/go/blob/master/src/go.mod

huangapple
  • 本文由 发表于 2021年6月1日 00:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/67777294.html
匿名

发表评论

匿名网友

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

确定