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

go
11,648

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

Share:
11,648

Related videos on Youtube

Author by

Doe John

Updated on June 26, 2022

Comments

  • Doe John over 1 year

    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?

    • jeevatkm about 5 years
      Typically if project is not under VCS (at-least locally) go mod won't be able to detect the module path. It better to create go.mod manually and mention module modulepathhere then save it or in your go source code mention import path comment like package main // import "github.com/user/modulename". Then run go mod init.
  • VasileM about 5 years
    Incomplete answer. How to use your own projects modules from local git repository?
  • Hamza Anis
    Hamza Anis about 5 years
    stackoverflow.com/a/52124448/4544967 To use the module locally please refer this answer. I'll update this answer too.
  • Hamza Anis
    Hamza Anis about 5 years
    @VasileM I hope this completes the answer.
  • VasileM about 5 years
    With this: ` replace "X/v2" v2.1.0 => "manually path"` you are pointing to any path (v1, v3, etc) distinct to v2. Where is the improvement of go mod to get your version automatically by git tag?
  • VasileM about 5 years
    With this option you are doing a lot of unnecessary work. You can simply omit modules and pointing to any path manually (it's fast)
  • Hamza Anis
    Hamza Anis about 5 years
    When I will be testing it locally then I should have known that which version I want to check. Tagging is enabled by default on remote repo but I am not sure for the local because the git tags are not pushed by default to the remote repo.
  • Hamza Anis
    Hamza Anis about 5 years
    @VasileM An edit would be appreciated in this answer. You can suggest me a better solution for it.