malformed module path "xxxx/xxxx/uuid" missing dot in first path element when migrating from GOPATH based dep to go mod
Asked Answered
V

6

33
$ 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?

Visor answered 20/10, 2019 at 13:49 Comment(1)
N
21

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.

Newfeld answered 25/10, 2019 at 18:29 Comment(0)
P
3

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.

Pangaro answered 9/12, 2019 at 15:5 Comment(0)
L
1

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

Labyrinthodont answered 26/10, 2020 at 16:6 Comment(0)
N
0

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.

Necessaries answered 4/5, 2022 at 15:44 Comment(0)
N
0

Very unusual scenario, but I was working with an incorrect/out-of-date symlink main.go. Linking it to the correct file fixed that error.

Natatorial answered 13/6, 2022 at 17:3 Comment(0)
C
-4

BECAUSE GO CANNOT FIND THE MODULE!!!

Do not put your project inside GOPATH... that is so confusing

Instead...

set in .bashrc: export GO111MODULE=on

close all command line terminals, then re-open terminals

go to the project root folder

$ go mod init project_module_name

This will generate go.mod file

Install your packages with the version you want: go get -v github.com/golang/[email protected]

Then run $ go run main.go

Now the go.mod file should record the package versions Golang will use to run and build...

Make sure your are using the correct package versions by checking the go.mod file! It should look like: github.com/golang/protobuf v1.3.4

Compel answered 19/3, 2020 at 7:47 Comment(4)
Do not put your project inside GOPATH... that is so confusing - That is a convention, which I particularly prefer.Visor
Also, the error is missing dot in first path element not cannot resolve moduleVisor
Ayush Gupta, the error is caused by incorrect module usageCompel
incorrect package versions have incorrect file path => cause Golang not able to find the correct file to run or buildCompel

© 2022 - 2024 — McMap. All rights reserved.