Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go module init without VCS/Git fails with cannot determine module path

Tags:

I'm trying to initialize a new go project with go module (using go 1.11). I don't plan to publish it in github or elsewhere, it is just a temporary/test project with only main package.

Whenever I try to run go mod init in a directory (that's outside my $GOPATH), I get this error:

go: cannot determine module path for source directory /Users/... (outside GOPATH, no import comments)

Is it not possible to init module without using git (or other VCS)? Or is there any workaround?

like image 779
Doe John Avatar asked Sep 01 '18 06:09

Doe John


People also ask

What is module path in golang?

A module path is the canonical name for a module, declared with the module directive in the module's go. mod file. A module's path is the prefix for package paths within the module. A module path should describe both what the module does and where to find it.

Is go mod necessary?

Dependency modules do not need to have explicit go. mod files. The “main module” in module mode — that is, the module containing the working directory for the go command — must have a go.

What is go mod init?

The go mod init command creates a go. mod file to track your code's dependencies. So far, the file includes only the name of your module and the Go version your code supports. But as you add dependencies, the go. mod file will list the versions your code depends on.

Where is go mod file?

What is Go Module. A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root. This file defines the module's path, which is also the import path used for your project, and its dependency requirements, which are the other modules needed for a successful build.


1 Answers

Is it not possible to init module without using git (or other VCS)? Or is there any workaround?

Yes, it is possible to init the modules without using VSC, initializing the module does not have to do anything with git or any other VCS.

This error occurs when the module name is not entered while init the module so to generate a module modulename write this command.

$ go mod init modulename 

The content of the go.mod would be

module modulename 

EDIT:

To use the modules from local repository use the replace directive

In your main module where you are checking your local module add the following lines

replace "X" v0.0.0 => "{location To your local module}" require "X" v0.0.0 

And then in your main project, import package util from module X you can simply do:

import "X/util" 

Now when you will do go build it will look for this local module on the location you have given in the mod file of the main project.

For more explanation

like image 69
Hamza Anis Avatar answered Oct 10 '22 15:10

Hamza Anis