'malformed module path "xxxx/xxxx/uuid" missing dot in first path element when migrating from GOPATH based dep to go mod
$ go version 1.13.3
I have a folder structure as follows:
GOPATH
+---src
+--- my-api-server
+--- my-auth-server
+--- main.go
+--- my-utils
+--- uuid
+--- uuid.go
my-auth-server
uses my-api-server/my-utils/uuid
as a depenency
Now, when I used the GOPATH based module system, this worked fine. But when using go modules, when I run go run main.go
in my-auth-server
it returned error:
build command-line-arguments: cannot load my-api-server/my-utils/uuid: malformed module path "my-api-server/my-utils/uuid": missing dot in first path element
Any idea how to solve this?
Solution 1:[1]
The go.mod
file should be at the root of your project (in this case, my-api-server/go.mod
).
The first part of the module path should be a domain/path. For example, the full path might be github.com/your-github-username/my-api-server
. The error you're seeing is because the first part is not a domain (with a period). You don't have to publish the module to develop it, but you need to use a proper domain name.
Once you have a module path, you can import packages contained in that module using the full module path + "/" + the relative path of the package. For example,
import "github.com/your-github-username/my-api-server/my-utils/uuid"
Since main.go
and uuid
are contained in the same module, you don't need a require
statement in the go.mod
file to use the uuid
package. You can import it like any other package and it will work.
I recommend using go build
and running the resulting executable rather than using go run
to make sure you include all of the files you need in the build process.
See https://blog.golang.org/using-go-modules for a walkthrough of how to use Go modules, including the second post in that series about how to convert a project to use modules.
Solution 2:[2]
Check your import paths on your main.go file.
I had to call the entire import path:
github.com/[username]/[project-name]/views
instead of:
[project-name]/views
to make it work on my end.
Solution 3:[3]
if you are trying to use a global package(non standard), there should be a dot('.') in the first part of package name. Probably expects dot('.') as it does in any URL, in this case github.com....which marks that as remote package.
If you want to use local package, then you need to use go module, then first part would be the name of you go module(name which you have used during initilization of your go module).
Example (as per the question):
Go to project root folder (in this case ../src$
) & run following command
go mod init myapiserver
This will create a go.mod & go.sum file.
Then to import uuid, you can simply use myapiserver/my-utils/uuid
in import
import "myapiserver/my-utils/uuid"
then all the public functions(which starts with Capital letters) from uuid.go
will be accessible in the current file
Solution 4:[4]
upgrade go version with the latest or greater than 1.16.1.
go version >=1.16.1 (required)
for mac -
brew update
brew upgrade golang
for linux - Remove Existing go, and Install the latest one.
This worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tyler Bui-Palsulich |
Solution 2 | |
Solution 3 | |
Solution 4 | anonymous |