SoFunction
Updated on 2025-03-03

Golang language http protocol get splicing parameter operation

I won't say much nonsense, let's just read the code~

package main
import (
	"fmt"
	"net/url"
)
// Manage the HTTP GET request parameters
type GetRequest struct {
	urls 
}
// Initializer
func (p *GetRequest) Init() *GetRequest {
	 = {}
	return p
}
// Initialized from another instance
func (p *GetRequest) InitFrom(reqParams *GetRequest) *GetRequest {
	if reqParams != nil {
		 = 
	} else {
		 = {}
	}
	return p
}
// Add URL escape property and value pair
func (p *GetRequest) AddParam(property string, value string) *GetRequest {
	if property != "" && value != "" {
		(property, value)
	}
	return p
}
// Concat the property and value pair
func (p *GetRequest) BuildParams() string {
	return ()
}
func main() {
	init := new(GetRequest).Init()
	params := ("market", "sh").AddParam("Inst","6000987").BuildParams()
	//params := ("market", "sh")
	(params)
}

result:

Inst=6000987&market=sh

Supplement: Common common processing methods for golang string splicing

Preface

In daily development, especially when connecting with third-party login, payment, sharing and other functions, we often need to sort and splice the data we receive, and then perform or encrypt or verify processing. Although the dead order can solve the problem, the code lacks flexibility.

From a general perspective, this article uses golang language to deal with the general string splicing problem.

It has been developed into a special splicing toolgo-join, welcome to use and give comments.

Splicing processing

Currently, there are two main sorting methods that are often encountered:

1. Parameter name ASCII code sorted from small to large

The following are the signature requirements for the WeChat developer platform:

The common steps for signature generation are as follows:

The first step is to assume that all the sent or received data are set M, and the parameters of non-null parameter values ​​in the set M are sorted from small to large according to the parameter name ASCII code (dictionary order), and use the format of the URL key-value pair (that is, key1=value1&key2=value2…) to splice into a string stringA.

Pay special attention to the following important rules:

◆ Parameter name ASCII codes are sorted from small to large (dictionary order);

◆ If the value of the parameter is empty, it will not participate in the signature;

◆ Parameter names are case sensitive;

◆ When the verification call returns or WeChat actively notifies the signature, the transmitted sign parameter does not participate in the signature, and the generated signature is verified with the sign value.

◆ The WeChat interface may add fields, and the added extended fields must be supported when verifying signatures.

//JoinStringsInASCII According to the rules, the parameter name ASCII code is sorted from small to large and then spliced.//data Data to be spliced//sep Connector//OnlyValues ​​does not contain only parameter values, and true does not contain parameter names, otherwise both parameter names and parameter values ​​are//IncludeEmpty does not contain null values, true will contain null values, otherwise it will not be included. Note that this parameter does not affect the existence of the parameter name//exceptKeys Excluded parameter names, do not participate in sorting and splicingfunc JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
 var list []string
 var keyList []string
 m := make(map[string]int)
 if len(exceptKeys) > 0 {
 for _, except := range exceptKeys {
  m[except] = 1
 }
 }
 for k := range data {
 if _, ok := m[k]; ok {
  continue
 }
 value := data[k]
 if !includeEmpty && value == "" {
  continue
 }
 if onlyValues {
  keyList = append(keyList, k)
 } else {
  list = append(list, ("%s=%s", k, value))
 }
 }
 if onlyValues {
 (keyList)
 for _,v := range keyList {
  list = append(list,data[v])
 }
 }else {
 (list)
 }
 return (list, sep)
}

Example:

 data := make(map[string]string)
 data["appid"] = "wx_1234535"
 data["body"] = "test data"
 data["mch_id"] = "572836589"
 data["notify_url"] = ""
 data["trade_type"] = "MWEB"
 data["spbill_create_ip"] = "192.169.0.1"
 data["total_fee"] = "100"
 data["out_trade_no"] = "2745890486870"
 data["nonce_str"] = "kdjskgjokghdk"
 data["sign"] = "signData"
 ("str :", JoinStringsInASCII(data, "&", false, false, "sign"))
 ("str2 :", JoinStringsInASCII(data, "&", true, false, "sign"))

Example results:

str : appid=wx_1234535&body=test data&mch_id=572836589&nonce_str=kdjskgjokghdk&notify_url=&out_trade_no=2745890486870&spbill_create_ip=192.169.0.1&total_fee=100&trade_type=MWEB
str2 : wx_1234535&test data&572836589&kdjskgjokghdk&&2745890486870&192.169.0.1&100&MWEB

2. Splice in specific parameter order

//JoinStringsInOrder Number of names are arranged in fixed order and then spliced//data Data to be spliced//sep Connector//OnlyValues ​​does not contain only parameter values, and true does not contain parameter names, otherwise both parameter names and parameter values ​​are//orders Parameter names in order, if not, do not participate in the splicingfunc JoinStringsInOrder(data map[string]string, sep string,onlyValues bool, orders ...string) string {
 var list []string
 for _, key := range orders {
 if _,ok := data[key];!ok {
  continue
 }
 if onlyValues {
  list = append(list, data[key])
 }else {
  list = append(list, ("%s=%s", key, data[key]))
 }
 }
 return (list, sep)
}

Example:

("str3 :", JoinStringsInOrder(data, "&", false, "appid","body","mch_id","notify_url"))

("str4 :", JoinStringsInOrder(data, "&", true, "appid","body","mch_id","notify_url"))

Example results:

str3 : appid=wx_1234535&body=test data&mch_id=572836589&notify_url=

str4 : wx_1234535&test data&572836589&

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.