首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Golang中导入本地模块

无法在Golang中导入本地模块
EN

Stack Overflow用户
提问于 2020-03-14 06:56:11
回答 4查看 34.8K关注 0票数 18

我试图导入本地模块,但我无法使用go mod导入它。我最初使用go mod int github.com/AP/Ch2-GOMS构建了我的项目

注意,我的环境是go1.14,我使用VSCode作为我的编辑器。

这是我的文件夹结构

代码语言:javascript
复制
Ch2-GOMS
│   ├── go.mod
│   ├── handlers
│   │   └── hello.go
│   └── main.go

我的main.go代码:

代码语言:javascript
复制
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)
} 

我无法看到本地模块(如handlers.NewHello )的自动完成。

go build生成go.mod内容:

代码语言:javascript
复制
module github.com/AP/Ch2-GOMS
go 1.14

我也得到了,你既不是在一个模块,也不是在你的GOPATH。有关如何设置Go项目的信息,请参阅 https://github.com/golang/go/wiki/Modules ,尽管已在~/.bashrc文件中设置GO111MODULE=on,但请在VScode中发出警告。

EN

回答 4

Stack Overflow用户

发布于 2020-03-14 08:41:02

阅读:伊恩·兰斯·泰勒的评论 (围棋的核心团队)

我知道三种方法:

  • 方法1 (最佳方法):
代码语言:javascript
复制
# 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"

你一定做错了什么,这就是为什么它不起作用的原因。

  • 方法2 (好方法):
代码语言:javascript
复制
# 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

在Ch2-GOMS/go.mod中添加以下行:

代码语言:javascript
复制
# 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
  • 方法3 (对于不耐烦的人来说是一种非常快速的攻击方式):
代码语言:javascript
复制
1. Turn off Go Modules `GO111MODULE=off`
2. Remove `go.mod` file

代码语言:javascript
复制
# 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

原因:在构建过程中,编译器首先查看供应商,然后查看GOPATH,然后查看GOROOT。因此,由于符号链接,VSCode的go相关工具也将正确工作,因为它依赖于GOPATH (它们在GOPATH之外不工作)

票数 18
EN

Stack Overflow用户

发布于 2021-05-29 14:11:45

以下是步骤-

代码语言:javascript
复制
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

在上述步骤之前,您的项目路径应该是

代码语言:javascript
复制
project path = GOPATH/src/<project_name>

同时还应该有两个与src文件夹并行的文件夹

  • src
  • pkg
  • 箱子

当您安装任何软件包时,它应该位于pkg文件夹中,并且在执行go mod整齐之后,应该生成一个文件。

  • go.mod
  • 列表项目
票数 1
EN

Stack Overflow用户

发布于 2021-10-06 08:12:48

只有go mod tidy在根文件夹为我做的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60680470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档