Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing local packages within a go module (go 1.11)

Tags:

go

vgo

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 
like image 732
David Alsh Avatar asked Aug 26 '18 12:08

David Alsh


People also ask

What is the difference between go module and package?

In Go, a package is a directory of .go files, and packages form the basic building blocks of a Go program. Using packages, you organize your code into reusable units. A module, on the other hand, is a collection of Go packages, with dependencies and versioning built-in.

Where does go look for packages?

Go first searches for package directory inside GOROOT/src directory and if it doesn't find the package, then it looks for GOPATH/src . Since, fmt package is part of Go's standard library which is located in GOROOT/src , it is imported from there.


2 Answers

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

like image 77
Muhammad Soliman Avatar answered Oct 04 '22 12:10

Muhammad Soliman


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" 
like image 23
Halil Kaskavalci Avatar answered Oct 04 '22 12:10

Halil Kaskavalci