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

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
}

我在 C:\users\...\go 的 GOPATH 之外构建它,我正在使用 go 1.16。

在阅读了一些资料说我可以只使用模块名称和包路径来导入包之后,我使用这一行将 handlers 包导入 main 包。

import "project1/handlers/handlers"

但是当我尝试 运行 时,这个 returns 这个错误。

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

然后我尝试更改一些东西,比如更改我的模块名称和导入路径到这个

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

//import path in main.go
import "github.com/username/project1/handlers/handlers"

但是 returns 错误说我需要先使用 go get github.com/username/project1/handlers/handlers 获取包,当我尝试使用该命令或只是简单地 go mod tidy returns repository not found 错误,因为我没有 push/publish 这个项目,我也不打算这样做。

那么,我在这里制作的 problem/mistake 是什么?我必须先发布我的项目才能导入我自己的本地包吗?我错过了一些配置吗?感谢您的每一次帮助。

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

import "project1/handlers"

//rather than
import "project1/handlers/handlers"

已解决问题。谢谢!