How does URL encoding ignore zero-value key pairs?
birdycn opened this issue · comments
birdycn commented
How does URL encoding ignore zero-value key pairs?
Glenn Lewis commented
Sorry, I'm not sure I understand the question. Can you please elaborate and/or give an example?
birdycn commented
对不起,我不确定我理解这个问题。你能详细说明和/或举例吗?
type Params struct{
Name string `url:name`
Age int `url:omitempty`
}
a:=&Params{Name:"Chen",Age:12}
b:=&Params{Name:"Chen"}
will get:
name=chen&age=12
name=chen
but now:
name=chen&age=12
name=chen&age=
Glenn Lewis commented
Note that your tag is not correct.
Please try this:
package main
import (
"log"
"github.com/google/go-querystring/query"
)
func main() {
type Params struct {
Name string `url:"name"`
Age int `url:"age,omitempty"`
}
a := &Params{Name: "Chen", Age: 12}
b := &Params{Name: "Chen"}
va, _ := query.Values(a)
log.Printf("a: %v", va.Encode())
vb, _ := query.Values(b)
log.Printf("b: %v", vb.Encode())
}
$ go run main.go
2019/04/29 10:56:24 a: age=12&name=Chen
2019/04/29 10:56:24 b: name=Chen
Do you still have a question, or does this solve the issue for you?
birdycn commented
请注意,您的代码不正确。
请试试这个:
包主 导入( “日志” “ github.com/google/go-querystring/query ” ) func main(){ type Params struct { 名称字符串 `网址: “名字” ` 年龄 INT `网址: “年龄,omitempty” ` } a :=&Params {姓名:“陈”,年龄:12 } b :=&Params {姓名:“陈” } va,_ :=查询。价值观(一) 登录。Printf(“ a:%v ”,va。编码()) vb,_ :=查询。价值观(b) 登录。Printf(“ b:%v ”,vb。编码()) }$ go run main.go 2019/04/29 10:56:24 a:年龄= 12 &姓名=陈 2019/04/29 10:56:24 b:姓名=陈你还有问题,还是为你解决问题?
thank! @
Glenn Lewis commented
That went extremely well for neither of us speaking each other's language!
Google Translate helped us out quite a bit! 😄