6

Golang:go-querystring将struct编码为URL查询参数的库

 2 years ago
source link: https://blog.51cto.com/u_13567403/5652359
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Golang:go-querystring将struct编码为URL查询参数的库

精选 原创

彭世瑜 2022-09-05 17:09:51 ©著作权

文章标签 github 数据结构 bash 文章分类 Go语言 编程语言 yyds干货盘点 阅读数410

go-querystring is a Go library for encoding structs into URL query parameters.

译文:go-querystring 将struct编码为URL查询参数的Golang库

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
}

参考
 「Go工具箱」推荐一个Google开发的将结构体快速拼接成url查询参数的工具

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK