google / go-querystring

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

Home Page:https://pkg.go.dev/github.com/google/go-querystring/query

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does URL encoding ignore zero-value key pairs?

birdycn opened this issue · comments

How does URL encoding ignore zero-value key pairs?

Sorry, I'm not sure I understand the question. Can you please elaborate and/or give an example?

对不起,我不确定我理解这个问题。你能详细说明和/或举例吗?

@gmlewis

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=

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?

请注意,您的代码不正确。

请试试这个:

包主

导入(
	 “日志”

	“ 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! @

That went extremely well for neither of us speaking each other's language!
Google Translate helped us out quite a bit! 😄