golang语言http协议get拼接参数

package main

import (
	"fmt"
	"net/url"
)

// Manage the HTTP GET request parameters
type GetRequest struct {
	urls url.Values
}

// Initializer
func (p *GetRequest) Init() *GetRequest {
	p.urls = url.Values{}
	return p
}

// Initialized from another instance
func (p *GetRequest) InitFrom(reqParams *GetRequest) *GetRequest {
	if reqParams != nil {
		p.urls = reqParams.urls
	} else {
		p.urls = url.Values{}
	}
	return p
}

// Add URL escape property and value pair
func (p *GetRequest) AddParam(property string, value string) *GetRequest {
	if property != "" && value != "" {
		p.urls.Add(property, value)
	}
	return p
}

// Concat the property and value pair
func (p *GetRequest) BuildParams() string {
	return p.urls.Encode()
}

func main() {
	init := new(GetRequest).Init()
	params := init.AddParam("market", "sh").AddParam("Inst","6000987").BuildParams()
	//params := init.AddParam("market", "sh")
	fmt.Println(params)
}

结果:Inst=6000987&market=sh

你可能感兴趣的:(golang)