Golang:go-querystring將struct編碼為URL查詢引數的庫
go-querystring is a Go library for encoding structs into URL query parameters.
譯文:go-querystring 將struct編碼為URL查詢引數的Golang庫
文件
- https://pkg.go.dev/github.com/google/go-querystring
安裝
go get github.com/google/go-querystring
示例
package main
import (
"fmt"
"github.com/google/go-querystring/query"
)
// 注意:資料結構屬性名需要大寫
type Data struct {
Name string `url:"name"`
Age int `url:"age"`
IsStudent bool `url:"isStudent"`
}
func main() {
data := Data{
Name: "Tom",
Age: 2,
IsStudent: true,
}
value, _ := query.Values(data)
output := value.Encode()
fmt.Println(output)
// age=2&isStudent=true&name=Tom
}
回覆:【golang加群】加入golang開發者交流群