149

I'm trying out Go's new modules system and am having trouble accessing local packages. The following project is in a folder on my desktop outside my gopath.

My project structure looks like:

/
  - /platform
      - platform.go
  - main.go
  - go.mod

// platform.go
package platform

import "fmt"

func Print() {
    fmt.Println("Hi")
}

// main.go
package main

import "platform"

func main() {
    platform.Print()
}

go build main.go tells me

cannot find module for path platform
David Alsh
  • 6,747
  • 6
  • 34
  • 60
  • 2
    Hi David, if you believe my response answers your question please consider marking it as the answer. Thanks! – Halil Oct 25 '18 at 07:01
  • maven golang plugin since 2.3.3 also allows to work with local modules (it automatically add local paths during build), you can take a look at some example https://github.com/raydac/mvn-golang/tree/master/mvn-golang-examples/mvn-golang-example-maven-module-mix – Igor Maznitsa Jul 31 '19 at 07:15

4 Answers4

216

Let me define this first modules are collections of packages. In Go 11, I use go modules like the following:

If both packages are in the same project, you could just do the following: In go.mod:

module github.com/userName/moduleName

and inside your main.go

import "github.com/userName/moduleName/platform"

However, if they are separate modules, i.e different physical paths and you still want to import local packages without publishing this remotely to github for example, you could achieve this by using replace directive.

Given the module name github.com/otherModule and platform, as you've called it, is the only package inside there. In your main module's go.mod add the following lines:

module github.com/userName/mainModule

require "github.com/userName/otherModule" v0.0.0
replace "github.com/userName/otherModule" v0.0.0 => "local physical path to the otherModule"

Note: The path should point to the root directory of the module, and can be absolute or relative.

Inside main.go, to import a specific package like platform from otherModule:

import "github.com/userName/otherModule/platform"

Here's a gentle introduction to Golang Modules

Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • 8
    OMG this answer helped me so much. Thanks :) – Adis Azhar Aug 18 '19 at 14:49
  • 3
    I have used a go.mod containing only the replace directive, the require directive was not required to build the project. –  Sep 26 '19 at 10:01
  • 1
    also good example: https://forum.golangbridge.org/t/go-module-and-importing-local-package/11649 – kokemomuke Feb 12 '20 at 09:01
  • 7
    my issue is why must module have github.com...the question is talking about local files, why must answer contain a url??? that is my issue here...is it difficult to use file path like other programming languages? or must we treat everything like external url? – uberrebu Apr 22 '21 at 22:42
  • 1
    @uberrebu neither the dot or a FQDN are required anymore in the most recent versions of Go (tested in 1.15). Read more about that in [the overview](https://github.com/golang-standards/project-layout#overview) section of this [excellent howto](https://github.com/golang-standards/project-layout). – ripat Jul 02 '21 at 18:40
  • 1
    @muhammad-soliman This works, but I can't figure out why `import "github.com/userName/moduleName/platform"` gets converted to `import "github.com/userName/moduleName/github.com/userName/moduleName/platform"` when I save the file in VSC. No replacements are needed in the _go.mod_ file. Note: go version 1.17.2. – datalifenyc Oct 18 '21 at 00:00
  • 1
    yes I did that, and it works, but now, if I do "go mod download" he can not download the local package >_ – Nikolai Ehrhardt Dec 28 '21 at 02:52
  • Coming from java & python, I find golang's packages and modules really confusing. This answer is a lifesaver ! – Kira Feb 18 '23 at 04:33
64

I would strongly suggest you to use go toolchain which takes care of these issues out of the box. Visual Studio Code with vscode-go plugin is really useful.

Problem here is that Go requires relative paths with respect to your $GOPATH/src or module in import statement. Depending on where you are in your GOPATH, import path should include that as well. In this case, import statement must include go module path in go.mod

GOPATH

Assume your project resides here:

$GOPATH/src/github.com/myuser/myproject

Your import path should be:

import "github.com/myuser/myproject/platform"

VGO

Assume your go.mod file is:

module example.com/myuser/myproject

Your import path should be:

import "example.com/myuser/myproject/platform"
Halil
  • 2,076
  • 1
  • 22
  • 30
  • This was bang on - in the case of go modules - I have to follow vgo's pattern, where I start the path of the import statement with the module's name. Thanks! – David Alsh Oct 30 '18 at 03:18
  • 23
    Wait, I'm confused. If my module is just somewhere on my file system, how does the module system find it? – Narfanator Apr 02 '19 at 17:51
  • 1
    If you are inside a module, it finds packages in the same module using the package import path & the location of the `go.mod` file on disk, as long as the import paths are full import paths that start with the module name (e.g., `import "github.com/my/module/pkg`"`). If you want to find packages in other modules that are located elsewhere on your disk, then that is where a [replace](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) directive comes in. This [answer](https://stackoverflow.com/a/57314494/11210494) covers more on module structure and import paths. – thepudds Aug 01 '19 at 19:38
  • 33
    Why everyone keep using url formats? Question was very clear: localmodule/localmodule.go that's it. Why there's githubs and example.com's in here? – holms Feb 28 '20 at 16:04
  • 1
    @holms the answer and the comment above yours explain it, but to clarify, the language has no mechanism to locate internal packages, only the go tool does. the go tool depends on either GOPATH or the module configuration (go.mod). you are, in essence, using the go tools ability to treat your local packages the same as _any other kind of package_. see also some of the other answers on this question. – user4893106 May 07 '20 at 22:07
  • 10
    my issue is why must module have `github.com`...the question is talking about local files, why must answer contain a url??? that is my issue here...is it difficult to use file path like other programming languages? or must we treat everything like external url? – uberrebu Apr 22 '21 at 22:41
  • go tool needs to know where to locate the source. It accepts a [URL by design](https://golang.org/ref/mod#module-path). If you want to access local go files, you can setup GOPATH and access local files under `$GOPATH/src` directory. If you want to avoid GOPATH, you can check [possible workarounds](https://github.com/golang/go/issues/37554) that can be used via modules. – Halil Apr 25 '21 at 12:41
48

As someone new to go I didn't immediately understand the accepted answer – which is great, by the way. Here's a shorter answer for those very new people!

In go modules/packages are expressed as urls that you import in your code:

import your.org/internal/fancy_module

But wait! My code isn't at a url, what's happening??

This is the cleverness of go. You pretend there's a url even when there isn't one. Because:

  1. This makes including easier as no matter where your file is located the import uses the same url (the code stays the same even if the files move!)
  2. You can have packages that having naming conflicts. So google.com/go-api/user doesn't conflict with the definitions at your.org/internal/user
  3. Someday you might publish a url on GitHub and all the code will just work

That's all great Evan, but how do I import a relative path?

Good question! You can import by changing your go.mod file to have this line:

module fancy_module

go 1.16

replace your.org/fancy_module => ../path/to/fancy_module
Evan Moran
  • 3,825
  • 34
  • 20
  • 4
    You are a lifesaver my dude! I know that "replace" is discouraged solely because it's bad practice, but this was just what I needed to salvage a somewhat outdated project. – madprogramer Nov 04 '21 at 13:01
  • 3
    Many thanks. I decided to show some "programming" to my kid and immediately faced an issue that Go does not see a local module. I don't want to explain what's GitHub to someone who does not know yet what "variable" means. – kivagant Feb 17 '22 at 01:27
  • 1
    @madprogramer you are quite welcome! I'm still feeling out `replace`, but I think it is likely used for local development and totally fine on stuff you are actively making! – Evan Moran Dec 14 '22 at 22:01
2

Given the Golang Project structure

/
  - /platform
      - platform.go
  - main.go
  - go.mod

To access the methods or structs...etc (which are public) from local packages of /platform is simple, shown below

// main.go package main

import (
p "./platform"
)

func main() {
    p.Print()
}

this should work

Madhu Cheepati
  • 809
  • 5
  • 12
  • 2
    While code-only answers might answer the question, you could significantly improve the quality of your answer by providing context for your code, a reason for why this code works, and some references to documentation for further reading. From [answer]: _"Brevity is acceptable, but fuller explanations are better."_ – Pranav Hosangadi May 10 '22 at 21:34
  • sure, make sense – Madhu Cheepati May 31 '22 at 22:35