您当前的位置: 首页 >  golang

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

彭世瑜 发布时间:2022-09-05 17:08:54 ,浏览量:3

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
}

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

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1402s