Skip to content

net/url: Stitching complex arrays #29703

@qingwg

Description

@qingwg

What version of Go are you using (go version)?

$ go version
go version go1.11.4 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/qingwuguo/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/qingwuguo/go"
GOPROXY=""
GORACE=""
GOROOT="/home/linuxbrew/.linuxbrew/Cellar/go/1.11.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/home/linuxbrew/.linuxbrew/Cellar/go/1.11.4/libexec/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc-5"
CXX="g++-5"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build223599200=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I want to get a query string.
If like this,I can

type B struct {
	ItemC string `json:"c"`
}

structB := B{
	ItemC: "c",
}

var params = url.Values{}
jsonbs, _ := json.Marshal(structB)
jsonmap := make(map[string]interface{})
json.Unmarshal(jsonbs, &jsonmap)
for k, v := range jsonmap {
	params.Add(k, fmt.Sprintf("%v", v))
}
fmt.Println(params.Encode())

Bug if complex arrays,like this

type A struct {
	ItemB B `json:"b"`
}

type B struct {
	ItemC string `json:"c"`
}

structB := B{
	ItemC: "c",
}
structA := A{
	ItemB: structB,
}

var params = url.Values{}
jsonbs, _ := json.Marshal(structA)
jsonmap := make(map[string]interface{})
json.Unmarshal(jsonbs, &jsonmap)
for k, v := range jsonmap {
	switch t := v.(type) {
	default:
		params.Add(k, fmt.Sprintf("%v", v))
	case map[string]interface{}:
		for kk, vv := range t {
			// this I do not know what to do
		}
	}
}
fmt.Println(params.Encode())

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions