Golang 筆記
  • Introduction
  • 安裝Golang
    • 本地編譯與安裝第三方套件
    • 第一個 GO 程式
    • export 與 import
    • 使用 go mod
  • 基本工具
  • DataBase操作
    • MySQL
    • 使用 ORM
    • MongoDB
  • 基本語法
    • Variable
    • BSON
    • JSON
    • 時間相關
    • Interface
    • Error
    • 型別
    • 字串
    • defer
    • panic, recover
    • Channel 與Goroutine
      • 讀寫鎖
      • for select 與 for range
      • UnBuffered channel 與 Buffered channel
    • Function
    • pointer
    • regExp
    • fmt
    • Make vs New
    • struct
    • Array, Slice 陣列
    • map
  • 核心模組
    • Reflect
    • File
    • Signal
    • BuiltIn
    • Sync
    • IO
    • Sort
    • http
    • crypto
    • context
  • 第三方模組
    • Dom Parser
    • gin 框架
    • Websocket
    • Iris框架
      • 讀取 Body 資料
      • 相關範例
    • Fiber 框架
    • 自動重啟 server 工具
    • go-jwt
  • 測試
  • 原始碼解析
  • 常見問題
    • 資料存取不正確
    • Data races
Powered by GitBook
On this page
  • 回傳 JSON
  • parse body
  • 取得 header
  • 加入 middleware
  • 取得 *http.Request
  • 取得 path params
  • 取得 query string
  • Form-data 檔案上傳
  • Swagger 文件整合

Was this helpful?

  1. 第三方模組

Fiber 框架

Previous相關範例Next自動重啟 server 工具

Last updated 3 years ago

Was this helpful?

類似於 Node.js express 寫法。

回傳 JSON

package main

import "github.com/gofiber/fiber"

func main() {
	app := fiber.New()

	app.Get("/", func(c *fiber.Ctx) {
		c.JSON(fiber.Map{"code": 200, "message": "Hello, World"})
	})

	app.Listen(3000)
}

parse body

	app.Post("/signup", func(c *fiber.Ctx) {
		fmt.Println(c.Body())
	})
	
	// name=test&city=ewe

使用 body parser

package main

import (
	"fmt"
	"log"

	"github.com/gofiber/fiber"
)

type User struct {
	Name string `json:"name"`
	City string `json:"city"`
}

func main() {
	app := fiber.New()
	app.Post("/signup", func(c *fiber.Ctx) {
		user := new(User)
		if err := c.BodyParser(user); err != nil {
			log.Fatal(err)
		}
		fmt.Println(user.Name)
	})
	app.Listen(3001)
}

struct 跟裡面的 field 記得要大寫

取得 header

使用 get

app.Post("/signup", func(c *fiber.Ctx) { 
  ...
  fmt.Println(c.Get("authorization"))

加入 middleware

類似於 express 用法,傳入 next

	app.Post("/message", func(c *fiber.Ctx) {
	 ....do something
		c.Next()
	}, func(c *fiber.Ctx) {
	 ....do something after
	}

或是

func Auth(c *fiber.Ctx) {
  ...
  c.Next()
}

app.Post("/message", Auth, func(c *fiber.Ctx) {
 ...
}

取得 *http.Request

使用 context() 方法即可取得

app.Post("/message", func(c *fiber.Ctx) {
 ...
 c.Context()
}

取得 path params

app.Get("/:name", func(c *fiber.Ctx) error {
    msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
    return c.SendString(msg)
})

取得 query string

app.Get("/", func(c *fiber.Ctx) error {
  c.Query("order")         // "desc"
  c.Query("brand")         // "nike"
  c.Query("empty", "nike") // "nike"
})

Form-data 檔案上傳

func UploadAvatar(c *fiber.Ctx) error {
	file, err := c.FormFile("document")

	// Save file to root directory:
	return c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
}

Swagger 文件整合

https://stackoverflow.com/a/24837507/4622645
https://ithelp.ithome.com.tw/articles/10224472
GitHub - gofiber/fiber: ⚡️ Express inspired web framework written in GoGitHub
ContextFiber
JSON-to-Go: Convert JSON to Go instantly
Logo
Logo