为什么我不能在同一个模块中导入包?

huangapple go评论119阅读模式
英文:

Why can't I import package in the same module?

问题

我在使用Golang时遇到了一些关于导入本地包的问题。这是我的项目结构:

  1. home/src/github.com/username/project1
  2. |main.go
  3. |go.mod
  4. ├── handlers
  5. ├── handlers.go
  6. ├── usecases
  7. | ├─ ...
  8. |...

我的go.mod文件:

  1. module project1
  2. go 1.16

我的main.go文件:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "project1/handlers/handlers"
  6. )
  7. func main() {
  8. http.HandleFunc("/", handlers.Greet)
  9. http.ListenAndServe(":8080", nil)
  10. }

我的handlers/handlers.go文件:

  1. package handlers
  2. import (
  3. "net/http"
  4. "fmt"
  5. )
  6. func Greet(w http.ResponseWriter, r *http.Request) {
  7. //Do stuff
  8. }

我正在GOPATH之外构建这个项目,GOPATH是C:\users\...\go,我正在使用go 1.16。

在阅读了一些资料后,我得知可以使用模块名和包的路径来导入本地包,所以我使用以下代码将handlers包导入到main包中:

  1. import "project1/handlers/handlers"

但是当我尝试运行时,出现了以下错误:

  1. package project1/handlers/handlers is not in GOROOT (C:\...\project1/handlers/handlers)

然后我尝试做了一些更改,将模块名和导入路径都改为以下内容:

  1. //go.mod中的模块名
  2. module github.com/username/project1
  3. //main.go中的导入路径
  4. import "github.com/username/project1/handlers/handlers"

但是这时又出现了错误,提示我需要先使用go get github.com/username/project1/handlers/handlers命令获取该包。但是当我尝试使用该命令或者简单地运行go mod tidy时,都会返回repository not found错误,因为我还没有推送/发布这个项目,而且我也没有计划这样做。

所以,我在这里犯了什么问题/错误?我是否需要在导入自己的本地包之前先发布我的项目?我是否错过了一些配置?非常感谢任何帮助。

英文:

I have some problem with Golang about importing local only package. This is my project structure

  1. home/src/github.com/username/project1
  2. |main.go
  3. |go.mod
  4. ├── handlers
  5. ├── handlers.go
  6. ├── usecases
  7. | ├─ ...
  8. |...

my go.mod

  1. module project1
  2. go 1.16

my main.go

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "project1/handlers/handlers"
  6. )
  7. func main() {
  8. http.HandleFunc("/", handlers.Greet)
  9. http.ListenAndServe(":8080", nil)
  10. }

my handlers/handlers.go

  1. package handlers
  2. import (
  3. "net/http"
  4. "fmt"
  5. )
  6. func Greet(w http.ResponseWriter, r *http.Request) {
  7. //Do stuff
  8. }

I'm building this outside the GOPATH which is C:\users\...\go and I am using go 1.16.

After reading some sources that said I can just use the module name and path to the package to import the package I use this line to import handlers package to main package.

  1. import "project1/handlers/handlers"

But this returns this error when I tried to run it.

  1. package project1/handlers/handlers is not in GOROOT (C:\...\project1/handlers/handlers)

Then I tried to change some stuff like changing both my module name and import path to this

  1. //module name in go.mod
  2. module github.com/username/project1
  3. //import path in main.go
  4. import "github.com/username/project1/handlers/handlers"

but then it returns error that said I need to get the package first with go get github.com/username/project1/handlers/handlers which when I tried to get it with that command or just simply go mod tidy returns repository not found error because I haven't push/publish this project and I'm not planning to do it.

So, what is the problem/mistake that I made here? do I have to publish my project before I can import my own local package? Did I miss some configuration? Every single help is appreciated.

答案1

得分: 3

我在导入路径中放了太多的路径,应该停留在目录而不是包上。所以通过将导入路径更改为

  1. import "project1/handlers"
  2. //而不是
  3. import "project1/handlers/handlers"

解决了这个问题。谢谢!

英文:

I put too many path in the import path, it should stop at the directory rather than package. So by changing the import path to

  1. import "project1/handlers"
  2. //rather than
  3. import "project1/handlers/handlers"

fixed the problem. Thanks!

huangapple
  • 本文由 发表于 2021年6月4日 14:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/67832659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定