Go Fiber 框架系列教程 01: 和 Express 對比學習

閱讀本文大概需要 10 分鐘。

大家好,我是 polarisxu。

每次發框架相關的文章,總有人提到 Go Fiber 框架。於是乎,學習了下 Fiber,感覺確實挺不錯的。因此寫下這個 Fiber 系列。

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

01 Fiber 框架

這是一個 Go 語言 Web 框架,啓發自 NodeJS 框架:Express。該框架基於 FastHTTP 構建,旨在簡化零內存分配提高性能,以便快速開發。

如果你是一位 NodeJS 開發者,想學習 Go,這個框架應該很適合你,同時這裏還有一份專門爲 NodeJS 開發者準備的 Go 學習資料:https://github.com/miguelmota/golang-for-nodejs-developers

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

從第三方性能測試結果看,Fiber 的表現比 Gin、Echo 好很多。這裏有詳細的 Benchmark 測試說明:https://docs.gofiber.io/extra/benchmarks。

摘抄一段官方關於 Fiber 的哲學:

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

Fiber 受到了互聯網上最流行的 Web 框架 Express 的啓發 。我們結合了 Express 的易用性和 Go 的原始性能 。如果您曾經在 Node.js 上實現過 Web 應用程序 (使用 Express 或類似工具),那麼許多方法和原理對您來說應該非常易懂。

我們關注 整個互聯網 用戶在 issues 和 Discord channel 的消息,爲了創建一個迅速,靈活以及友好的 Go Web 框架,滿足任何任務,最後期限和開發者技能。就像 Express 在 JavaScript 世界中一樣。

所以,總結一下 Fiber 的特點(優勢):

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

02 和 Express 的簡短比較

既然是受 Express 啓發,那就和它比較下。

Hello World

基於 Express 的 Hello World 程序:

const express = require("express"); // 引用 Express library
const app = express(); // 創建一個 Express 實例

// 路由:/ endpoint
app.get("/"(req, res) ={
  res.send("Hello World!");
});

// 在 3000 端口啓動服務
app.listen(3000);

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

現在用 Fiber 實現類似上面的功能:

package main

import "github.com/gofiber/fiber/v2" // 注意,最新版本是 v2.18.0,所以有 v2

func main() {
  app := fiber.New() // 創建一個 Fiber 實例

  // 路由:/ endpoint
  app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
  })

  // 在 3000 端口啓動服務
  app.Listen(":3000")
}

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

Fiber 啓動後終端的輸出結果:

$ go run main.go

 ┌───────────────────────────────────────────────────┐
 │                   Fiber v2.18.0                   │
 │               http://127.0.0.1:3000               │
 │       (bound on host 0.0.0.0 and port 3000)       │
 │                                                   │
 │ Handlers ............. 2  Processes ........... 1 │
 │ Prefork ....... Disabled  PID ............. 83538 │
 └───────────────────────────────────────────────────┘

路由和端點

任何 Web 應用程序、微服務或 API 都包含一個基於描述 HTTP 方法的端點(endpoint)和處理程序函數的路由系統,只有在這個端點接收到客戶端的請求後纔會執行這個路由系統。

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

// Endpoint for POST method
app.post("/"(req, res) ={
  // function that stores a new data
});

// Endpoint for PUT method
app.put("/"(req, res) ={
  // function that replaces the existing data
});

// Endpoint for PATCH method
app.patch("/"(req, res) ={
  // function that replaces part of the existing data
});

// Endpoint for DELETE method
app.delete("/"(req, res) ={
  // function that deletes the data
});

對應的 Fiber 代碼:

// Endpoint for Post method
app.Post("/", func(c *fiber.Ctx) error {
  // function that stores a new data
})

// Endpoint for PUT method
app.Put("/", func(c *fiber.Ctx) error {
  // function that replaces the existing data
})

// Endpoint for PATH method
app.Path("/", func(c *fiber.Ctx) error {
  // function that replaces part of the existing data
})

// Endpoint for DELETE method
app.Delete("/", func(c *fiber.Ctx) error {
  // function that deletes the data
})

中間件

中間件函數可以訪問 HTTP 請求和響應對象,以及調用下一個中間件函數。一般地,中間件函數執行如下動作:

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

app.use(function (req, res, next) {
  // 打印當前時間
  console.log("Date:", Date.now());

  next();
});

對應 Fiber 的代碼如下:

app.Use(func(c *fiber.Ctx) error {
  // 打印當前時間
  fmt.Println("Date:", time.Now())

  return c.Next()
})

服務靜態文件

Web 應用經常會有靜態文件,它們需要能夠被請求,比如圖片、css/js 文件等。

服務靜態文件,一般基於如下幾個點:

看看 Express 如何做到的:

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

對應 Fiber 的代碼如下:

app.Static(
  "/static",  // mount address
  "./public", // path to the file folder
)

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

http://localhost:3000/static/images/background.jpg 對應是 public/images/background.jpg 文件

使用模板

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

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

app.set("view engine""pug");

// 初始化模板文件夾
app.set("views""./views");

app.get("/"(req, res) ={
  res.render("index"{
    title: "Hey!",
    message: "This is the index template.",
  });
});

對應的 Fiber 代碼如下(注意,Fiber 對模板的支持是 https://github.com/gofiber/template 包):

// 基於 ./views 文件夾初始化 Pug 模板引擎
engine := pug.New("./views"".pug")

app := fiber.New(fiber.Config{
  Views: engine, // 設置模板引擎
})

app.Get("/", func(c *fiber.Ctx) error {
  return c.Render("index", fiber.Map{
    "Title":   "Hey!",
    "Message""This is the index template.",
  })
})

03 小結

本文簡單介紹了 Fiber 的一些特性。因爲 Fiber 是受 Express 啓發實現的,因此和 Express 進行了對比。不知道你對 Fiber 有什麼感覺?

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

參考

我是 polarisxu,北大碩士畢業,曾在 360 等知名互聯網公司工作,10 多年技術研發與架構經驗!2012 年接觸 Go 語言並創建了 Go 語言中文網!著有《Go 語言編程之旅》、開源圖書《Go 語言標準庫》等。

堅持輸出技術(包括 Go、Rust 等技術)、職場心得和創業感悟!歡迎關注「polarisxu」一起成長!也歡迎加我微信好友交流:gopherstudio

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/AUdKsBpqwOU5wJPyAP0fgw