Oliver Williams
Oliver Williams

Reputation: 6334

Use Absolute path instead for go "fmt" in import directive

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!

Upvotes: 0

Views: 353

Answers (2)

Zombo
Zombo

Reputation: 1

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

Upvotes: 0

Thundercat
Thundercat

Reputation: 120931

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.

Upvotes: 3

Related Questions