自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go Fiber 框架系列之一: 和 Express 對比學(xué)習(xí)

開發(fā) 后端
這是一個 Go 語言 Web 框架,啟發(fā)自 NodeJS 框架:Express。該框架基于 FastHTTP 構(gòu)建,旨在簡化、零內(nèi)存分配和提高性能,以便快速開發(fā)。

[[425569]]

大家好,我是 polarisxu。

每次發(fā)框架相關(guān)的文章,總有人提到 Go Fiber 框架。于是乎,學(xué)習(xí)了下 Fiber,感覺確實挺不錯的。因此寫下這個 Fiber 系列。

Fiber 項目地址:https://github.com/gofiber/fiber,目前 Star 數(shù) 15.3k+。

01 Fiber 框架

這是一個 Go 語言 Web 框架,啟發(fā)自 NodeJS 框架:Express。該框架基于 FastHTTP 構(gòu)建,旨在簡化、零內(nèi)存分配和提高性能,以便快速開發(fā)。

如果你是一位 NodeJS 開發(fā)者,想學(xué)習(xí) Go,這個框架應(yīng)該很適合你,同時這里還有一份專門為 NodeJS 開發(fā)者準(zhǔn)備的 Go 學(xué)習(xí)資料:https://github.com/miguelmota/golang-for-nodejs-developers

這個框架是 2020 年 1 月份啟動開發(fā)的,沒想到短時間就受到很多人關(guān)注。從 README 的多國語言就可見一斑:

從第三方性能測試結(jié)果看,F(xiàn)iber 的表現(xiàn)比 Gin、Echo 好很多。這里有詳細(xì)的 Benchmark 測試說明:https://docs.gofiber.io/extra/benchmarks。

摘抄一段官方關(guān)于 Fiber 的哲學(xué):

Fiber 作為一個 Web 框架 ,是按照極簡主義的思想并遵循 UNIX 方式創(chuàng)建的,因此新的 gopher 可以在熱烈和可信賴的歡迎中迅速進(jìn)入 Go 的世界。

Fiber 受到了互聯(lián)網(wǎng)上最流行的 Web 框架 Express 的啟發(fā) 。我們結(jié)合了 Express 的易用性和 Go 的原始性能 。如果您曾經(jīng)在 Node.js 上實現(xiàn)過 Web 應(yīng)用程序(使用 Express 或類似工具),那么許多方法和原理對您來說應(yīng)該非常易懂。

我們關(guān)注 整個互聯(lián)網(wǎng) 用戶在 issues 和 Discord channel 的消息,為了創(chuàng)建一個迅速,靈活以及友好的 Go Web 框架,滿足任何任務(wù),最后期限和開發(fā)者技能。就像 Express 在 JavaScript 世界中一樣。

所以,總結(jié)一下 Fiber 的特點(diǎn)(優(yōu)勢):

  • 強(qiáng)大的路由
  • 靜態(tài)文件服務(wù)
  • 極致高性能
  • 內(nèi)存占用低
  • API 接口
  • 中間件和 Next 支持
  • 快速服務(wù)器端編程
  • 支持各種模版引擎
  • WebSocket 支持
  • 頻率限制器
  • 文檔被翻譯為 16 種語言

不過有兩點(diǎn)需要注意,F(xiàn)iber 使用了 unsafe 和 fasthttp,所以可能和 Go 最新版本有兼容性問題。目前 Fiber 2.18.0 兼容 Go 1.14 到 Go1.17;但 fasthttp 和 net/http 是不兼容的,因此 net/http 生態(tài)的項目無法使用在 fiber 上。

02 和 Express 的簡短比較

既然是受 Express 啟發(fā),那就和它比較下。

Hello World

基于 Express 的 Hello World 程序:

  1. const express = require("express"); // 引用 Express library 
  2. const app = express(); // 創(chuàng)建一個 Express 實例 
  3.  
  4. // 路由:/ endpoint 
  5. app.get("/", (req, res) => { 
  6.   res.send("Hello World!"); 
  7. }); 
  8.  
  9. // 在 3000 端口啟動服務(wù) 
  10. app.listen(3000); 

確實挺簡單,幾行代碼就搞定了一個 Web 服務(wù)。

現(xiàn)在用 Fiber 實現(xiàn)類似上面的功能:

  1. package main 
  2.  
  3. import "github.com/gofiber/fiber/v2" // 注意,最新版本是 v2.18.0,所以有 v2 
  4.  
  5. func main() { 
  6.   app := fiber.New() // 創(chuàng)建一個 Fiber 實例 
  7.  
  8.   // 路由:/ endpoint 
  9.   app.Get("/", func(c *fiber.Ctx) error { 
  10.     return c.SendString("Hello, World!"
  11.   }) 
  12.  
  13.   // 在 3000 端口啟動服務(wù) 
  14.   app.Listen(":3000"

目前,幾乎所有 Go 框架都是類似的路子,沒有太多好解釋的。

Fiber 啟動后終端的輸出結(jié)果:

  1. $ go run main.go 
  2.  
  3.  ┌───────────────────────────────────────────────────┐ 
  4.  │                   Fiber v2.18.0                   │ 
  5.  │               http://127.0.0.1:3000               │ 
  6.  │       (bound on host 0.0.0.0 and port 3000)       │ 
  7.  │                                                   │ 
  8.  │ Handlers ............. 2  Processes ........... 1 │ 
  9.  │ Prefork ....... Disabled  PID ............. 83538 │ 
  10.  └───────────────────────────────────────────────────┘ 

路由和端點(diǎn)

任何 Web 應(yīng)用程序、微服務(wù)或 API 都包含一個基于描述 HTTP 方法的端點(diǎn)(endpoint)和處理程序函數(shù)的路由系統(tǒng),只有在這個端點(diǎn)接收到客戶端的請求后才會執(zhí)行這個路由系統(tǒng)。

除了上面的 HTTP GET 方法,Express 和 Fiber 還支持其他 HTTP 基本方法(當(dāng)然還支持其他 HTTP 方法)。

  1. // Endpoint for POST method 
  2. app.post("/", (req, res) => { 
  3.   // function that stores a new data 
  4. }); 
  5.  
  6. // Endpoint for PUT method 
  7. app.put("/", (req, res) => { 
  8.   // function that replaces the existing data 
  9. }); 
  10.  
  11. // Endpoint for PATCH method 
  12. app.patch("/", (req, res) => { 
  13.   // function that replaces part of the existing data 
  14. }); 
  15.  
  16. // Endpoint for DELETE method 
  17. app.delete("/", (req, res) => { 
  18.   // function that deletes the data 
  19. }); 

對應(yīng)的 Fiber 代碼:

  1. // Endpoint for Post method 
  2. app.Post("/", func(c *fiber.Ctx) error { 
  3.   // function that stores a new data 
  4. }) 
  5.  
  6. // Endpoint for PUT method 
  7. app.Put("/", func(c *fiber.Ctx) error { 
  8.   // function that replaces the existing data 
  9. }) 
  10.  
  11. // Endpoint for PATH method 
  12. app.Path("/", func(c *fiber.Ctx) error { 
  13.   // function that replaces part of the existing data 
  14. }) 
  15.  
  16. // Endpoint for DELETE method 
  17. app.Delete("/", func(c *fiber.Ctx) error { 
  18.   // function that deletes the data 
  19. }) 

中間件

中間件函數(shù)可以訪問 HTTP 請求和響應(yīng)對象,以及調(diào)用下一個中間件函數(shù)。一般地,中間件函數(shù)執(zhí)行如下動作:

  • 執(zhí)行我們想讓其執(zhí)行的代碼
  • 對請求或響應(yīng)對象做任何修改
  • 完成請求-響應(yīng)循環(huán)
  • 調(diào)用堆棧中的下一個中間件函數(shù)

看一個中間件的例子,它們在 Express 和 Fiber 中如何寫。

  1. app.use(function (req, res, next) { 
  2.   // 打印當(dāng)前時間 
  3.   console.log("Date:"Date.now()); 
  4.  
  5.   next(); 
  6. }); 

對應(yīng) Fiber 的代碼如下:

  1. app.Use(func(c *fiber.Ctx) error { 
  2.   // 打印當(dāng)前時間 
  3.   fmt.Println("Date:"time.Now()) 
  4.  
  5.   return c.Next() 
  6. }) 

服務(wù)靜態(tài)文件

Web 應(yīng)用經(jīng)常會有靜態(tài)文件,它們需要能夠被請求,比如圖片、css/js 文件等。

服務(wù)靜態(tài)文件,一般基于如下幾個點(diǎn):

  • 一個存儲靜態(tài)文件的文件夾
  • 在 Web 程序中指定掛載點(diǎn)
  • 對掛載點(diǎn)進(jìn)行引用

看看 Express 如何做到的:

  1. app.use( 
  2.   "/static", // mount address 
  3.   express.static("public") // path to the file folder 
  4. ); 

對應(yīng) Fiber 的代碼如下:

  1. app.Static
  2.   "/static",  // mount address 
  3.   "./public", // path to the file folder 

因此,我們對 /static/ 下的文件訪問,都對應(yīng)到 public 下的文件。比如:

http://localhost:3000/static/images/background.jpg 對應(yīng)是 public/images/background.jpg 文件

使用模板

目前,Go 很多框架對各種模板引擎支持是不夠的。但 Fiber 做到了和 Express 類似,支持大量開箱即用的模板引擎,比如:Pug、Jade、Mustache 和 Handlebars 等。

以 Pug 為例,看看 Express 和 Fiber 如何使用的。(注意,以下代碼會查找 ./views 目錄下的 index.pug 文件,沒有該文件會報錯)

  1. app.set("view engine""pug"); 
  2.  
  3. // 初始化模板文件夾 
  4. app.set("views""./views"); 
  5.  
  6. app.get("/", (req, res) => { 
  7.   res.render("index", { 
  8.     title: "Hey!"
  9.     message: "This is the index template."
  10.   }); 
  11. }); 

對應(yīng)的 Fiber 代碼如下(注意,F(xiàn)iber 對模板的支持是 https://github.com/gofiber/template 包):

  1. // 基于 ./views 文件夾初始化 Pug 模板引擎 
  2. engine := pug.New("./views"".pug"
  3.  
  4. app := fiber.New(fiber.Config{ 
  5.   Views: engine, // 設(shè)置模板引擎 
  6. }) 
  7.  
  8. app.Get("/", func(c *fiber.Ctx) error { 
  9.   return c.Render("index", fiber.Map{ 
  10.     "Title":   "Hey!"
  11.     "Message""This is the index template."
  12.   }) 
  13. }) 

03 小結(jié)

本文簡單介紹了 Fiber 的一些特性。因為 Fiber 是受 Express 啟發(fā)實現(xiàn)的,因此和 Express 進(jìn)行了對比。不知道你對 Fiber 有什么感覺?

下篇文章會較詳細(xì)的介紹 Fiber 的一些特性。

參考

https://dev.to/koddr/go-fiber-by-examples-how-can-the-fiber-web-framework-be-useful-487a

本文轉(zhuǎn)載自微信公眾號「polarisxu」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系polarisxu公眾號。

 

https://docs.gofiber.io/api/fiber

 

責(zé)任編輯:武曉燕 來源: polarisxu
相關(guān)推薦

2021-10-06 19:03:35

Go中間件Middleware

2021-10-12 18:32:17

Go框架測試

2024-01-04 07:02:36

GoLangFiber開發(fā)

2020-09-16 12:18:28

GoJava模式

2022-02-28 14:54:48

openHarmon鴻蒙操作系統(tǒng)

2024-12-12 08:57:47

2018-03-12 22:13:46

GO語言編程軟件

2022-02-09 14:36:25

GoMongoDBFiber

2009-06-01 11:28:48

EquinoxOSGi入門

2022-01-07 15:11:27

項目Go 框架

2020-08-28 17:54:31

深度學(xué)習(xí)框架

2023-10-22 20:20:37

FiberGo

2021-02-04 15:08:37

Vue漸進(jìn)式框架

2021-07-07 09:18:00

Java并發(fā)編程

2010-08-06 10:46:20

IGRPRIP

2021-10-19 11:22:08

SentinelGo源碼

2017-05-05 10:15:38

深度學(xué)習(xí)框架對比分析

2019-03-06 09:55:54

Python 開發(fā)編程語言

2021-01-11 05:18:11

機(jī)器學(xué)習(xí)

2019-01-10 08:33:17

物聯(lián)網(wǎng)IoT機(jī)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號