Import local Go module in Goland IDE
Asked Answered
A

2

6

What is proper way to import (use) go module internally?

For example:

creating a new module by: go mod init example.com/my-project (example.com or another domain, which doesn't exist)

and using it in the same project:

import (
    "example.com/my-project/package"
)

This sample was taken from Let's Go book (but extremely simplified)

The problem:

I am using Goland, and It doesn't recognize this local module. Goland advice me to use go get -t example.com/my-project/package command. But example.com/... just a name for a module

enter image description here

Amnion answered 13/5, 2019 at 17:46 Comment(4)
Yeah, that's how that works. What is your question about it?Gisele
What problem did you have? Within the same module, go will find the source as expected.Jeopardy
@Adrian, I have extended my answer. And that extension describes the problem.Amnion
That looks like an issue with Goland. Have you verified that it works with the go tools themselves?Jeopardy
A
13

As @JimB mentioned, the problem related to Goland IDE. I found solution here.

Enabling Go modules (vgo) integration solves my problem.

enter image description here

Amnion answered 13/5, 2019 at 18:31 Comment(0)
W
1

The accepted answer did not help me.

Assume we have two modules in the project (two folders)

go mod init example.com/module1
go mod init example.com/module2

To able to import module1 from module2 we need to add following code in module2/go.mod:

require "example.com/module1" v0.0.0
replace "example.com/module1" => "../my-module"
Willable answered 28/12, 2021 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.