亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在控制器中導入模型

如何在控制器中導入模型

Go
慕蓋茨4494581 2022-09-19 21:28:23
下面是我的代碼的模型。package modelstype Goal struct {  Id        int    `json:"id"`  Title     string `json:"title"`  Status        bool   `json:"status"`}當我在控制器中導入模型包并想要使用時,它會給我一個錯誤。package controllersimport (    "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)}但我不想在控制器代碼中使用模型代碼。我想將模型保留在模型文件夾中。我想將模型和控制器保存在單獨的文件夾中。我認為我在控制器中導入模型包或模型中導入控制器包時做錯了什么。我不明白我該怎么做。你們能幫幫我嗎?提前感謝您。
查看完整描述

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{}


查看完整回答
反對 回復 2022-09-19
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號