如何在控制器中导入模型?

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

how to import models in controllers

问题

以下是我的代码模型。

  1. package models
  2. type Goal struct {
  3. Id int `json:"id"`
  4. Title string `json:"title"`
  5. Status bool `json:"status"`
  6. }

当我在控制器中导入models包并想要使用它时,它给我一个错误。

  1. package controllers
  2. import (
  3. "strconv"
  4. "github.com/gofiber/fiber/v2"
  5. "github.com/RohitKuwar/go_fiber/models" //错误:"github.com/RohitKuwar/go_fiber/models"导入但未使用
  6. )
  7. var goals = []models.Goal{ //错误:未声明名称:Goal
  8. {
  9. Id: 1,
  10. Title: "Read about Promises",
  11. Status: true,
  12. },
  13. {
  14. Id: 2,
  15. Title: "Read about Closures",
  16. Status: false,
  17. },
  18. }
  19. func GetGoals(c *fiber.Ctx) error {
  20. return c.Status(fiber.StatusOK).JSON(goals)
  21. }
  22. func CreateGoal(c *fiber.Ctx) error {
  23. type Request struct {
  24. Title string `json:"title"`
  25. Status string `json:"status"`
  26. }
  27. var body Request
  28. err := c.BodyParser(&body)
  29. // 如果出错
  30. if err != nil {
  31. return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
  32. "message": "无法解析JSON",
  33. "error": err,
  34. })
  35. }
  36. // 创建一个目标变量
  37. goal := &models.Goal{ //错误:未声明名称:Goal
  38. Id: len(goals) + 1,
  39. Title: body.Title,
  40. Status: body.Status,
  41. }

但是当我在控制器中像下面这样写models时,一切都正常工作。

  1. import (
  2. "strconv"
  3. "github.com/gofiber/fiber/v2"
  4. )
  5. type Goal struct {
  6. Id int `json:"id"`
  7. Title string `json:"title"`
  8. Status string `json:"status"`
  9. }
  10. var goals = []Goal{
  11. {
  12. Id: 1,
  13. Title: "Read about Promises",
  14. Status: "completed",
  15. },
  16. {
  17. Id: 2,
  18. Title: "Read about Closures",
  19. Status: "active",
  20. },
  21. }
  22. func GetGoals(c *fiber.Ctx) error {
  23. return c.Status(fiber.StatusOK).JSON(goals)
  24. }

但是我不想在控制器代码中使用models代码。我想将模型保留在models文件夹中。我想将models和controllers放在不同的文件夹中。我认为在控制器中导入models包或在models中导入controllers包时我做错了什么。我不明白如何做到这一点。你们能帮帮我吗?提前谢谢。

英文:

Below is the model for my code.

  1. package models
  2. type Goal struct {
  3. Id int `json:"id"`
  4. Title string `json:"title"`
  5. Status bool `json:"status"`
  6. }

When I import models package in controllers and want to use so it give me an errror.

  1. package controllers
  2. import (
  3. "strconv"
  4. "github.com/gofiber/fiber/v2"
  5. "github.com/RohitKuwar/go_fiber/models" //error:"github.com/RohitKuwar/go_fiber/models" imported but not used
  6. )
  7. var goals = []Goal{ //error:undeclared name: Goal
  8. {
  9. Id: 1,
  10. Title: "Read about Promises",
  11. Status: "completed",
  12. },
  13. {
  14. Id: 2,
  15. Title: "Read about Closures",
  16. Status: "active",
  17. },
  18. }
  19. func GetGoals(c *fiber.Ctx) error {
  20. return c.Status(fiber.StatusOK).JSON(goals)
  21. }
  22. func CreateGoal(c *fiber.Ctx) error {
  23. type Request struct {
  24. Title string `json:"title"`
  25. Status string `json:"status"`
  26. }
  27. var body Request
  28. err := c.BodyParser(&body)
  29. // if error
  30. if err != nil {
  31. return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
  32. "message": "Cannot parse JSON",
  33. "error": err,
  34. })
  35. }
  36. // create a goal variable
  37. goal := &Goal{ //error:undeclared name: Goal
  38. Id: len(goals) + 1,
  39. Title: body.Title,
  40. Status: body.Status,
  41. }

But when I write models in the controller like below then everything works fine.

  1. import (
  2. "strconv"
  3. "github.com/gofiber/fiber/v2"
  4. )
  5. type Goal struct {
  6. Id int `json:"id"`
  7. Title string `json:"title"`
  8. Status string `json:"status"`
  9. }
  10. var goals = []Goal{
  11. {
  12. Id: 1,
  13. Title: "Read about Promises",
  14. Status: "completed",
  15. },
  16. {
  17. Id: 2,
  18. Title: "Read about Closures",
  19. Status: "active",
  20. },
  21. }
  22. func GetGoals(c *fiber.Ctx) error {
  23. return c.Status(fiber.StatusOK).JSON(goals)
  24. }

But I do not want to use models code in controllers code. I want to keep the model in the models folder. I want to keep models and controllers in separate folders. I think I am doing something wrong while importing models package in controllers or controllers package in models. I am not understanding how can I do it. Can you guys please help me? Thank you in advance.

答案1

得分: 0

你的go.mod文件是什么样子的?我尝试使用以下目录结构和go.mod中的模块条目来运行你的代码,看起来像这样:module rishabh96b/go_fiber

  1. go_fiber tree .
  2. .
  3. ├── controllers
  4. └── mycontroller.go
  5. ├── go.mod
  6. ├── go.sum
  7. └── models
  8. └── goals.go

在我的情况下,成功导入了控制器中的models包。下面是mycontroller.go文件的内容:

  1. package controllers
  2. import (
  3. . "rishabh96b/go_fiber/models"
  4. "github.com/gofiber/fiber/v2"
  5. )
  6. var goals = []Goal{ //错误:未声明的名称:Goal
  7. {
  8. Id: 1,
  9. Title: "Read about Promises",
  10. Status: "completed",
  11. },
  12. {
  13. Id: 2,
  14. Title: "Read about Closures",
  15. Status: "active",
  16. },
  17. }
  18. func GetGoals(c *fiber.Ctx) error {
  19. return c.Status(fiber.StatusOK).JSON(goals)
  20. }
  21. func CreateGoal(c *fiber.Ctx) error {
  22. type Request struct {
  23. Title string `json:"title"`
  24. Status string `json:"status"`
  25. }
  26. var body Request
  27. err := c.BodyParser(&body)
  28. // 如果出错
  29. if err != nil {
  30. return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
  31. "message": "无法解析JSON",
  32. "error": err,
  33. })
  34. }
  35. // 创建一个goal变量
  36. goal := &Goal{ //错误:未声明的名称:Goal
  37. Id: len(goals) + 1,
  38. Title: body.Title,
  39. Status: body.Status,
  40. }
  41. }

P.S:"rishabh96b/go_fiber/models"前面的.意味着我不必在这里使用包名与结构体一起使用。简而言之,我可以简单地使用Goal{}而不是models.Goal{}

英文:

How does your go.mod file looks like? I tried to work your code with the following directory structure with the module entry in go.mod looking like module rishabh96b/go_fiber

  1. go_fiber tree .
  2. .
  3. ├── controllers
  4. └── mycontroller.go
  5. ├── go.mod
  6. ├── go.sum
  7. └── models
  8. └── goals.go

Importing models package in controller is successful in my case. Here is how mycontroller.go file looks like

  1. package controllers
  2. import (
  3. . "rishabh96b/go_fiber/models"
  4. "github.com/gofiber/fiber/v2"
  5. )
  6. var goals = []Goal{ //error:undeclared name: Goal
  7. {
  8. Id: 1,
  9. Title: "Read about Promises",
  10. Status: "completed",
  11. },
  12. {
  13. Id: 2,
  14. Title: "Read about Closures",
  15. Status: "active",
  16. },
  17. }
  18. func GetGoals(c *fiber.Ctx) error {
  19. return c.Status(fiber.StatusOK).JSON(goals)
  20. }
  21. func CreateGoal(c *fiber.Ctx) error {
  22. type Request struct {
  23. Title string `json:"title"`
  24. Status string `json:"status"`
  25. }
  26. var body Request
  27. err := c.BodyParser(&body)
  28. // if error
  29. if err != nil {
  30. return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
  31. "message": "Cannot parse JSON",
  32. "error": err,
  33. })
  34. }
  35. // create a goal variable
  36. goal := &Goal{ //error:undeclared name: Goal
  37. Id: len(goals) + 1,
  38. Title: body.Title,
  39. Status: body.Status,
  40. }
  41. }

P.S: The . in front of "rishabh96b/go_fiber/models" means that I don't have to use package name with struct I am using here. In short, I can simply use Goal{} instead of models.Goal{}

huangapple
  • 本文由 发表于 2021年8月19日 15:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/68843808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定