简体   繁体   中英

Could not import local modules in Golang

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 int 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

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)

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

go mod tidy单独在根文件夹为我做了

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

I found that the value of the package declaration must match the name of the folder, otherwise the import will fail. This is a low level mistake, but I hope it helps someone who makes the same mistake as me.

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

In my example, the package value I declared in hello.go is hello, not handlers, which leads to my import failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM