Could not import local modules in Golang
Asked Answered
B

4

24

I am trying to import local modules, but I am unable to import it using go mod. I initially built my project using go mod init github.com/AP/Ch2-GOMS

Note my environment is go1.14 and I am using VSCode as my editor.

This is my folder structure

Ch2-GOMS
│   ├── go.mod
│   ├── handlers
│   │   └── hello.go
│   └── main.go

My main.go code:

package main

import (
    "log"
    "net/http"
    "os"

    "github.com/AP/Ch2-GOMS/handlers" // This gives "could not import github.com/AP/Ch2-GOMS/handlers" lint error
)

func main() {

    l := log.New(os.Stdout, "product-api", log.LstdFlags)
    hh := handlers.NewHello(l)

    sm := http.NewServeMux()
    sm.Handle("/", hh)

    http.ListenAndServe(":9090", nil)
} 

I cannot see auto-complete for my local modules such as handlers.NewHello.

go build generated go.mod contents:

module github.com/AP/Ch2-GOMS
go 1.14

I am also getting You are neither in a module nor in your GOPATH. Please see https://github.com/golang/go/wiki/Modules for information on how to set up your Go project. warning in VScode, even though i have set GO111MODULE=on in my ~/.bashrc file

Berseem answered 14/3, 2020 at 6:56 Comment(1)
Show the output of go build. Stop using IDEs the moment you encounter fundamental problems and fall back to std tooling.Chemotherapy
F
25

Read: Ian Lance Taylor's comment (Go's Core Team)

I know of three ways:

  • Method 1 (The best way):
# Inside
# Ch2-GOMS
# │   ├── go.mod
# │   ├── handlers
# │   │   └── hello.go
# │   └── main.go

# In Ch2-GOMS
go mod init github.com/AP/Ch2-GOMS

# In main.go
# Add import "github.com/AP/Ch2-GOMS/handlers"
# But, make sure: 
# handlers/hello.go has a package name "package handlers"

You must be doing something wrong and that's why it's not working.

  • Method 2 (The good way):
# Inside
# Ch2-GOMS
# │   ├── go.mod
# │   ├── handlers
# │   │   └── hello.go
# │   └── main.go

# Inside the handlers package
cd Ch2-GOMS/handlers
go mod init github.com/AP/Ch2-GOMS/handlers # Generates go.mod
go build # Updates go.mod and go.sum

# Change directory to top-level (Ch2-GOMS)
cd ..
go mod init github.com/AP/Ch2-GOMS # Skip if already done
go build # Must fail for github.com/AP/Ch2-GOMS/handlers
vi go.mod

Inside Ch2-GOMS/go.mod and add the following line:

# Open go.mod for editing and add the below line at the bottom (Not inside require)
replace github.com/AP/Ch2-GOMS/handlers => ./handlers

# replace asks to replace the mentioned package with the path that you mentioned
# so it won't further look packages elsewhere and would look inside that's handlers package located there itself
  • Method 3 (The very quick hack way for the impatient):

    1. Turn off Go Modules GO111MODULE=off
    2. Remove go.mod file
# Check: echo $GOPATH

# If $GOPATH is set
mkdir -p $GOPATH/src/github.com/AP/Ch2-GOMS
cd $GOPATH/src/github.com/AP/Ch2-GOMS


# If $GOPATH is unset
mkdir -p ~/go/src/github.com/AP/Ch2-GOMS
cd ~/go/src/github.com/AP/Ch2-GOMS

# Now create a symbolic link
ln -s <full path to your package> handlers

Reason: During the build, the compiler first looks in vendor, then GOPATH, then GOROOT. So, due to the symlink, VSCode's go related tools will also work correctly due to the symlink provided as it relies on GOPATH (They don't work outside of GOPATH)

Fleeta answered 14/3, 2020 at 8:41 Comment(6)
Changed hello.go to handlers.go and it worked. Loved your 2 appraches, 1 right and the other wrong.Berseem
As I told you, it's the quick hack way (obviously, it's incorrect). I use the symlinks so that Go tooling works in VSCode. Also on the second note, the way you solved it now is not gonna scale. What if you several files belonging to the same package, and some more questions. Method 1, will help you in the long run I suppose, right?Fleeta
If it helps anyone this solution worked for me. I had copied a go.mod and go.sum file from another project along with the code into mine and it kept trying to access files in the other repo. Running go mod init ... fixed the issue.Grindery
If I want to create a test (hello_test.go) for hello.go inside a test folder, where should that folder be placed (w.r.t Method 2)Shoemake
Method 1. Delete go.mod file before executing go mod init github.com/AP/Ch2-GOMS. The command go mod init will not execute if go.mod already exists.Floats
for the method 1, yes! i'm running my go code in a python virtual env, when i moved the code to an new & clean directory, everything's okFrisch
M
5

If you want to import local modules, you need to map the module path such that it can find the code in your local file system.

First use the go mod edit command to replace any imports to module to the local file

$ go mod edit -replace example.com/greetings=../greetings

The command specifies that example.com/greetings should be replaced with ../greetings for the purpose of locating the dependency. After you run the command, the go.mod file in the current directory should include a replace directive in its mod file

After that use the go mod tidy command to synchronize dependencies, adding those required by code where you imported but not yet traced by the current module

$ go mod tidy

Referred from the official documentation

Misvalue answered 1/1, 2022 at 15:29 Comment(0)
T
1

Below are the steps-

on main folder - go mod init
2.go mod tidy
3.go to the folder where main file is present
4.install the package via 
    go get <package name>
5.go build

Before above steps your project path should be

project path = GOPATH/src/<project_name>

Along with there should be 2 more folder parallel with src folder

  • src
  • pkg
  • bin

when ever you install any package it should be go inside pkg folder and after doing go mod tidy there should be one file generated

  • go.mod
  • List item
Transpacific answered 29/5, 2021 at 14:11 Comment(0)
M
1

go mod tidy alone at root folder did it for me

Merits answered 6/10, 2021 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.