导入了包但明明使用了包却没有使用

Package imported but not used even if I clearly used the package

我不明白为什么在 GO 中导入本地包是这样的皮塔饼。他们经常抛出“包已导入但未使用”错误,即使我清楚地在同一个文件中使用了包!然后我使用它的代码抛出一个“未声明的名称”错误,就好像我没有导入它但不知何故你无法识别。

谁能解决我的问题?这不是第一次发生在我身上。很多时候我只是玩玩直到它有效,但现在它变得非常烦人。

// go.mod

module final-project
// cmd/main.go

import (
    "final-project/infra"
    "final-project/handler"
)

func main() {
    db := config.DBInit()
    inDB := &handler.CommentHandler{DB: db}
}
// infra/config.go
package infra

import (
    "fmt"

    "final-project/entity"

    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)


func DBInit() *gorm.DB {
    dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

    if err != nil {
        panic("Failed to connect to database")
    }

    fmt.Println("Database connected")

    db.AutoMigrate(structs.Comment{})
    return db
}

// handler/comments.go
package handler

import "net/http"

type CommentHandler struct{
    ///
}

func (u CommentHandler) Create(w http.ResponseWriter, r *http.Request) {
    ///
}
// entity/structs.go
package entity

import "time"

type Comment struct {
    ID uint
    Message   string
    CreatedAt time.Time
    UpdatedAt time.Time
}

这发生在我尝试从“最终项目”模块导入任何包的所有代码行中。因此,我使用 db := config.DBInit()(从 final-project/infra 导入)、db.AutoMigrate(structs.Comment{})(从 final-project/entity 导入)等包的所有代码都会抛出 undeclared name 错误(即使如您所见,我已尝试导入它)。为什么我的代码无法识别进口商品?谁能帮帮我?

你输入错了,不应该是文件名

db := config.DBInit()

应该是包名

db := infra.DBInit()

structs.Comment{}也是一样,应该是entity.Comment{}