1 回答

TA貢獻2011條經驗 獲得超2個贊
您的文件看起來如何?我試圖使用以下目錄結構處理您的代碼,模塊條目看起來像go.modgo.modmodule rishabh96b/go_fiber
? go_fiber tree .
.
├── controllers
│ └── mycontroller.go
├── go.mod
├── go.sum
└── models
└── goals.go
在我的情況下,在控制器中導入模型包是成功的。這是文件的外觀mycontroller.go
package controllers
import (
. "rishabh96b/go_fiber/models"
"github.com/gofiber/fiber/v2"
)
var goals = []Goal{ //error:undeclared name: Goal
{
Id: 1,
Title: "Read about Promises",
Status: "completed",
},
{
Id: 2,
Title: "Read about Closures",
Status: "active",
},
}
func GetGoals(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(goals)
}
func CreateGoal(c *fiber.Ctx) error {
type Request struct {
Title string `json:"title"`
Status string `json:"status"`
}
var body Request
err := c.BodyParser(&body)
// if error
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Cannot parse JSON",
"error": err,
})
}
// create a goal variable
goal := &Goal{ //error:undeclared name: Goal
Id: len(goals) + 1,
Title: body.Title,
Status: body.Status,
}
}
P.S:前面的意味著我不必使用我在這里使用的結構的包名稱。簡而言之,我可以簡單地使用而不是."rishabh96b/go_fiber/models"Goal{}models.Goal{}
- 1 回答
- 0 關注
- 96 瀏覽
添加回答
舉報