如何在控制器中导入模型
how to import models in controllers
下面是我的代码模型。
package models
type Goal struct {
Id int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
当我在控制器中导入模型包并想使用时,它给我一个错误。
package controllers
import (
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/RohitKuwar/go_fiber/models" //error:"github.com/RohitKuwar/go_fiber/models" imported but not used
)
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,
}
但是当我像下面这样在控制器中编写模型时,一切正常。
import (
"strconv"
"github.com/gofiber/fiber/v2"
)
type Goal struct {
Id int `json:"id"`
Title string `json:"title"`
Status string `json:"status"`
}
var goals = []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)
}
但我不想在控制器代码中使用模型代码。我想将模型保存在模型文件夹中。我想将模型和控制器保存在不同的文件夹中。我认为我在控制器中导入模型包或在模型中导入控制器包时做错了什么。我不明白我该怎么做。你们能帮帮我吗?提前谢谢你。
您的 go.mod
文件看起来如何?我尝试使用以下目录结构处理您的代码,go.mod
中的模块条目看起来像 module 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{}
下面是我的代码模型。
package models
type Goal struct {
Id int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
当我在控制器中导入模型包并想使用时,它给我一个错误。
package controllers
import (
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/RohitKuwar/go_fiber/models" //error:"github.com/RohitKuwar/go_fiber/models" imported but not used
)
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,
}
但是当我像下面这样在控制器中编写模型时,一切正常。
import (
"strconv"
"github.com/gofiber/fiber/v2"
)
type Goal struct {
Id int `json:"id"`
Title string `json:"title"`
Status string `json:"status"`
}
var goals = []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)
}
但我不想在控制器代码中使用模型代码。我想将模型保存在模型文件夹中。我想将模型和控制器保存在不同的文件夹中。我认为我在控制器中导入模型包或在模型中导入控制器包时做错了什么。我不明白我该怎么做。你们能帮帮我吗?提前谢谢你。
您的 go.mod
文件看起来如何?我尝试使用以下目录结构处理您的代码,go.mod
中的模块条目看起来像 module 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{}