22

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?

Doe John
  • 231
  • 1
  • 2
  • 6
  • 1
    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`. – jeevatkm Sep 01 '18 at 06:31

1 Answers1

31

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

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
  • Incomplete answer. How to use your own projects modules from local git repository? – VasileM Oct 15 '18 at 12:34
  • 1
    https://stackoverflow.com/a/52124448/4544967 To use the module locally please refer this answer. I'll update this answer too. – Hamza Anis Oct 15 '18 at 14:02
  • 1
    @VasileM I hope this completes the answer. – Hamza Anis Oct 15 '18 at 14:10
  • 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 Oct 15 '18 at 15:12
  • 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) – VasileM Oct 15 '18 at 15:17
  • 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 Oct 16 '18 at 13:05
  • @VasileM An edit would be appreciated in this answer. You can suggest me a better solution for it. – Hamza Anis Oct 16 '18 at 13:06