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

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

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

问题

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

home/src/github.com/username/project1
|main.go
|go.mod
├── handlers
│   ├── handlers.go
├── usecases
|   ├─ ...
|...

我的go.mod文件:

module project1

go 1.16

我的main.go文件:

package main

import (
	"fmt"
	"net/http"
	
	"project1/handlers/handlers"
)

func main() {

	http.HandleFunc("/", handlers.Greet)
	http.ListenAndServe(":8080", nil)

}

我的handlers/handlers.go文件:

package handlers

import (
	"net/http"
	"fmt"
)

func Greet(w http.ResponseWriter, r *http.Request) {
	//Do stuff
}

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

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

import "project1/handlers/handlers"

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

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

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

//go.mod中的模块名
module github.com/username/project1

//main.go中的导入路径
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

home/src/github.com/username/project1
|main.go
|go.mod
├── handlers
│   ├── handlers.go
├── usecases
|   ├─ ...
|...

my go.mod

module project1

go 1.16

my main.go

package main

import (
	"fmt"
	"net/http"
	
	"project1/handlers/handlers"
)

func main() {

	http.HandleFunc("/", handlers.Greet)
	http.ListenAndServe(":8080", nil)

}

my handlers/handlers.go

package handlers

import (
	"net/http"
	"fmt"
)

func Greet(w http.ResponseWriter, r *http.Request) {
	//Do stuff
}

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.

import "project1/handlers/handlers"

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

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

//module name in go.mod
module github.com/username/project1

//import path in main.go
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

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

import "project1/handlers"

//而不是
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

import "project1/handlers"

//rather than
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:

确定