Forcing a specific import path in Go

The New Go programmer here is an apology if this is a well-worn area, but my Google search did not find the answer I'm looking for.

Short version. Can I, as a programmer, external to the main Go project, force to import my packages with a specific name. If so, how?

Long version: I recently tried installing a package bcryptfrom the next GitHub repository with the followinggo get

go get github.com/golang/crypto

The package loaded correctly in my workspace, but when I tried to import it, I got the following error

$ go run main.go main.go: 10: 2: code in the /path/to/go/src/github.com/golang/crypto/bcrypt directory awaiting import "golang.org/x/crypto/bcrypt"

i.e. Something said that this package should have been imported using golang.org/x/crypto/bcrypt. This pushed me away from what I really wanted:

go get golang.org/x/crypto/bcrypt

I would like to do something similar in my own packages - is this feature built into the Go packaging? Or do authors crypto/bcryptdo something at runtime to detect and reject invalid package import names?

+4
source share
1 answer

Yes, it is built-in, I can not find an implementation document (this is a relatively new feature in 1.5 or 1.6), however the syntax is:

package name // import "your-custom-path"

Example: https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go#L7

// edit

The document for this function is https://docs.google.com/document/d/1jVFkZTcYbNLaTxXD9OcGfn7vYv5hWtPx9--lTx1gPMs/edit

// edit

@JimB https://golang.org/cmd/go/#hdr-Import_path_checking, go1.4: <3 >

+6

All Articles