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

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

how to import models in controllers

问题

以下是我的代码模型。

package models

type Goal struct {
  Id        int    `json:"id"`
  Title     string `json:"title"`
  Status    bool   `json:"status"`
}

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

package controllers

import (
	"strconv"
	"github.com/gofiber/fiber/v2"
	"github.com/RohitKuwar/go_fiber/models"  //错误:"github.com/RohitKuwar/go_fiber/models"导入但未使用
)

var goals = []models.Goal{     //错误:未声明名称:Goal
	{
		Id:        1,
		Title:     "Read about Promises",
		Status:    true,
	},
	{
		Id:        2,
		Title:     "Read about Closures",
		Status:    false,
	},
}

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 err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"message": "无法解析JSON",
			"error":   err,
		})
	}

	// 创建一个目标变量
	goal := &models.Goal{     //错误:未声明名称:Goal
		Id:        len(goals) + 1,
		Title:     body.Title,
		Status:    body.Status,
	}

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

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

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

英文:

Below is the model for my code.

package models

type Goal struct {
  Id        int    `json:"id"`
  Title     string `json:"title"`
  Status 		bool   `json:"status"`
}

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

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

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

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

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

➜  go_fiber tree .
.
├── controllers
│   └── mycontroller.go
├── go.mod
├── go.sum
└── models
    └── goals.go

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

package controllers

import (
    . "rishabh96b/go_fiber/models"

    "github.com/gofiber/fiber/v2"
)

var goals = []Goal{ //错误:未声明的名称: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 err != nil {
	return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
		"message": "无法解析JSON",
		"error":   err,
	})
}

// 创建一个goal变量
goal := &Goal{ //错误:未声明的名称:Goal
	Id:     len(goals) + 1,
	Title:  body.Title,
	Status: body.Status,
    }
}

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

➜  go_fiber tree .
.
├── controllers
│   └── mycontroller.go
├── go.mod
├── go.sum
└── models
    └── goals.go

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

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: 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:

确定