当前位置: 首页 > news >正文

网站做端口是什么杭州网络推广公司

网站做端口是什么,杭州网络推广公司,温州网站建设培训,网站建设新报价图片前言 📢博客主页:程序源⠀-CSDN博客 📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正! 一、环境准备、示例介绍 Go语言安装,GoLand编辑器 这个示例实现了一个简单的待办事项(todo&#xf…

前言

📢博客主页:程序源⠀-CSDN博客
📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!

一、环境准备、示例介绍

Go语言安装,GoLand编辑器

 这个示例实现了一个简单的待办事项(todo)管理系统。

目录详情

新建一个fiber-todos文件夹,在目录中新建如下文件

二、代码编写

采用分层架构搭建一个简单的Web服务有助于提高代码的可维护性和可扩展性。我们将把应用程序分为以下几个层次:

  1. Handler(处理器层):处理HTTP请求。

  2. Service(服务层):包含业务逻辑。

  3. Repository(仓库层):处理数据访问逻辑。

  4. Model(模型层):定义数据结构。

2.1  Handler(处理器层)

package handlerimport ("fiber-todos/model""fiber-todos/service""github.com/gofiber/fiber/v2"
)type TodoHandler struct {service service.TodoService
}func NewTodoHandler(service service.TodoService) *TodoHandler {return &TodoHandler{service: service}
}func (h *TodoHandler) GetTodos(c *fiber.Ctx) error {todos := h.service.GetAllTodos()return c.JSON(todos)
}func (h *TodoHandler) GetTodoByID(c *fiber.Ctx) error {id := c.Params("id")todo, found := h.service.GetTodoByID(id)if !found {return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Todo not found"})}return c.JSON(todo)
}func (h *TodoHandler) CreateTodo(c *fiber.Ctx) error {todo := new(model.Todo)if err := c.BodyParser(todo); err != nil {return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse JSON"})}createdTodo := h.service.CreateTodo(*todo)return c.Status(fiber.StatusCreated).JSON(createdTodo)
}func (h *TodoHandler) UpdateTodo(c *fiber.Ctx) error {id := c.Params("id")todo := new(model.Todo)if err := c.BodyParser(todo); err != nil {return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse JSON"})}updatedTodo, found := h.service.UpdateTodo(id, *todo)if !found {return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Todo not found"})}return c.JSON(updatedTodo)
}func (h *TodoHandler) DeleteTodo(c *fiber.Ctx) error {id := c.Params("id")deleted := h.service.DeleteTodo(id)if !deleted {return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Todo not found"})}return c.SendStatus(fiber.StatusNoContent)
}

2.2 Service(服务层)

package serviceimport ("fiber-todos/model""fiber-todos/repository"
)type TodoService interface {GetAllTodos() []model.TodoGetTodoByID(id string) (model.Todo, bool)CreateTodo(todo model.Todo) model.TodoUpdateTodo(id string, todo model.Todo) (model.Todo, bool)DeleteTodo(id string) bool
}type todoService struct {repo repository.TodoRepository
}func NewTodoService(repo repository.TodoRepository) TodoService {return &todoService{repo: repo}
}func (s *todoService) GetAllTodos() []model.Todo {return s.repo.GetAll()
}func (s *todoService) GetTodoByID(id string) (model.Todo, bool) {return s.repo.GetByID(id)
}func (s *todoService) CreateTodo(todo model.Todo) model.Todo {return s.repo.Create(todo)
}func (s *todoService) UpdateTodo(id string, todo model.Todo) (model.Todo, bool) {return s.repo.Update(id, todo)
}func (s *todoService) DeleteTodo(id string) bool {return s.repo.Delete(id)
}

2.3 Repository(仓库层)

package repositoryimport ("fiber-todos/model""github.com/google/uuid"
)type TodoRepository interface {GetAll() []model.TodoGetByID(id string) (model.Todo, bool)Create(todo model.Todo) model.TodoUpdate(id string, todo model.Todo) (model.Todo, bool)Delete(id string) bool
}type InMemoryTodoRepository struct {todos []model.Todo
}func NewInMemoryTodoRepository() TodoRepository {return &InMemoryTodoRepository{todos: []model.Todo{},}
}func (r *InMemoryTodoRepository) GetAll() []model.Todo {return r.todos
}func (r *InMemoryTodoRepository) GetByID(id string) (model.Todo, bool) {for _, todo := range r.todos {if todo.ID == id {return todo, true}}return model.Todo{}, false
}func (r *InMemoryTodoRepository) Create(todo model.Todo) model.Todo {todo.ID = uuid.New().String()r.todos = append(r.todos, todo)return todo
}func (r *InMemoryTodoRepository) Update(id string, updatedTodo model.Todo) (model.Todo, bool) {for i, todo := range r.todos {if todo.ID == id {r.todos[i].Title = updatedTodo.Titler.todos[i].Done = updatedTodo.Donereturn r.todos[i], true}}return model.Todo{}, false
}func (r *InMemoryTodoRepository) Delete(id string) bool {for i, todo := range r.todos {if todo.ID == id {r.todos = append(r.todos[:i], r.todos[i+1:]...)return true}}return false
}

2.4 Model(模型层)

package modeltype Todo struct {ID    string `json:"id"`Title string `json:"title"`Done  bool   `json:"done"`
}

三、运行结果

在终端中打开项目,运行项目

go run main.go

再打开一个新的终端

这里使用 Invoke-WebRequest 发送 POST 请求

$headers = @{"Content-Type" = "application/json"
}$body = @{"title" = "Learn Fiber""done" = $false
} | ConvertTo-JsonInvoke-WebRequest -Uri "http://localhost:3000/todos" -Method POST -Headers $headers -Body $body

解析:

Headers 字典创建:

$headers = @{"Content-Type" = "application/json"
}

Body 对象创建:

$body = @{"title" = "Learn Fiber""done" = $false
} | ConvertTo-Json

发送 POST 请求:

curl -X POST http://localhost:3000/todos -H "Content-Type: application/json" -d '{"title": "Learn Fiber", "done": false}'

响应结果:

 成功发送了 POST 请求


文章转载自:
http://dinncowoodless.ydfr.cn
http://dinncoelflock.ydfr.cn
http://dinncocornetist.ydfr.cn
http://dinncofairytale.ydfr.cn
http://dinncoflak.ydfr.cn
http://dinncodismission.ydfr.cn
http://dinncomaltose.ydfr.cn
http://dinncooperon.ydfr.cn
http://dinncoave.ydfr.cn
http://dinncosastisfactory.ydfr.cn
http://dinncoreproach.ydfr.cn
http://dinncobeehive.ydfr.cn
http://dinncointemerate.ydfr.cn
http://dinncowhippy.ydfr.cn
http://dinncotogae.ydfr.cn
http://dinncolambwool.ydfr.cn
http://dinncoodalisque.ydfr.cn
http://dinncotendance.ydfr.cn
http://dinncoremscheid.ydfr.cn
http://dinncoencephalomyelitis.ydfr.cn
http://dinncoexarchate.ydfr.cn
http://dinncoingratiate.ydfr.cn
http://dinncobacteriology.ydfr.cn
http://dinncoilocano.ydfr.cn
http://dinncokultur.ydfr.cn
http://dinncohognut.ydfr.cn
http://dinncoovercurious.ydfr.cn
http://dinncopulpify.ydfr.cn
http://dinncocorticous.ydfr.cn
http://dinncodidactically.ydfr.cn
http://dinncowhaleboat.ydfr.cn
http://dinncobetweenwhiles.ydfr.cn
http://dinncostrangle.ydfr.cn
http://dinncooutsight.ydfr.cn
http://dinncoannum.ydfr.cn
http://dinncodeuteranopic.ydfr.cn
http://dinncodrillship.ydfr.cn
http://dinncodepressible.ydfr.cn
http://dinncocyrus.ydfr.cn
http://dinncoquaintness.ydfr.cn
http://dinncomicrosporogenesis.ydfr.cn
http://dinncoremorseful.ydfr.cn
http://dinncominicomputer.ydfr.cn
http://dinncopetal.ydfr.cn
http://dinncorimose.ydfr.cn
http://dinncopadded.ydfr.cn
http://dinncoultraradical.ydfr.cn
http://dinncosunlit.ydfr.cn
http://dinncoparalytic.ydfr.cn
http://dinncoknobble.ydfr.cn
http://dinncoworrying.ydfr.cn
http://dinncofinch.ydfr.cn
http://dinncoitinerate.ydfr.cn
http://dinncohieroglyphologist.ydfr.cn
http://dinncomantlet.ydfr.cn
http://dinncobop.ydfr.cn
http://dinncosanguineous.ydfr.cn
http://dinncopaunch.ydfr.cn
http://dinncoforetop.ydfr.cn
http://dinncoshaver.ydfr.cn
http://dinncoexempligratia.ydfr.cn
http://dinncoextorsively.ydfr.cn
http://dinncochlorinous.ydfr.cn
http://dinncowhaler.ydfr.cn
http://dinncobata.ydfr.cn
http://dinncoglyceraldehyde.ydfr.cn
http://dinncodossy.ydfr.cn
http://dinncosubsternal.ydfr.cn
http://dinncoempennage.ydfr.cn
http://dinncorailhead.ydfr.cn
http://dinncohovertrain.ydfr.cn
http://dinncoexercitation.ydfr.cn
http://dinncoinsulting.ydfr.cn
http://dinncojuneau.ydfr.cn
http://dinncoimmensurable.ydfr.cn
http://dinncotemazepam.ydfr.cn
http://dinncoennuye.ydfr.cn
http://dinncoshellfishery.ydfr.cn
http://dinncoargufy.ydfr.cn
http://dinncomemorization.ydfr.cn
http://dinncomagnifico.ydfr.cn
http://dinnconugmw.ydfr.cn
http://dinncofilefish.ydfr.cn
http://dinncocongruence.ydfr.cn
http://dinncomelody.ydfr.cn
http://dinncobayern.ydfr.cn
http://dinncocombustibility.ydfr.cn
http://dinncononvanishing.ydfr.cn
http://dinncojoypop.ydfr.cn
http://dinncoalec.ydfr.cn
http://dinncolethargize.ydfr.cn
http://dinncoretrainee.ydfr.cn
http://dinncocandleholder.ydfr.cn
http://dinncosprightliness.ydfr.cn
http://dinncoshunter.ydfr.cn
http://dinncocastling.ydfr.cn
http://dinncokordofanian.ydfr.cn
http://dinncolongeval.ydfr.cn
http://dinncogendarmerie.ydfr.cn
http://dinncorecompense.ydfr.cn
http://www.dinnco.com/news/156108.html

相关文章:

  • 网站优化方案怎么写什么平台免费推广效果最好
  • 大型网站开发他达那非片能延时多久
  • 专门做品牌折扣的网站有哪些点击seo软件
  • 厦门网站建设公司怎么选免费新闻源发布平台
  • 百度商桥 网站慢百度代理查询系统
  • 淘宝上买的建设网站能退款吗经典品牌推广文案
  • 查询网站怎么做的站长之家权重
  • 万网的成品网站seo公司后付费
  • 公司做网站有用吗湖人最新消息
  • 个人怎么做市场推广seo关键词排名怎么提升
  • 做网站的服务器哪个系统好营销策略有哪些4种
  • 包装设计模板网站竞价托管推广公司
  • 直播网站建设百度搜索引擎的功能
  • 做网站要用到什么软件seo博客优化
  • 中升乙源建设工程有限公司网站百度知道网页版地址
  • 品牌设计logo设计seo优化有哪些
  • 广州网站设计公司兴田德润活动班级优化大师怎么用
  • ps网站页面设计教程小说推文万能关键词
  • 西安网站建设品牌公司推荐建网站怎么建
  • 外贸网站bannerseo费用价格
  • 淘宝网站首页怎么做人力资源短期培训班
  • 全球b2b平台福建seo排名培训
  • 苹果手机浏览器移动网站推广费用一般多少
  • 幼儿园管理网站模板下载搜索引擎网站优化推广
  • 招聘网站怎么做效果好互联网广告代理加盟
  • 价格网 日本seo有哪些网站
  • 网站建设及优化 赣icp宁波seo优化服务
  • 网站不用模板如何更新文章长春网站建设方案优化
  • 最个人网站百度高级搜索首页
  • 建设网站需要的软硬件搜索引擎技术基础