Requirement Scenario
When a backend service calls a third-party API, a common requirement is to build a URL query string. net/url is available in the go standard package to solve this problem, url.Values
is essentially a map[string][] string
, and provides a series of methods (Add, Del, Set) to manipulate the parameters, and eventually converts the map to a URL query string via the Encode()
method.
However, there is some repetitive work involved, such as.
- type conversion, to int, bool, etc. to string
- to determine whether the field is empty or zero value of the processing logic
For this problem, google open source go-querystring can solve this kind of repetitive work elegantly and concisely.
Usage
The entire go-querystring library exposes only one method, func Values(v interface{}) (url.Values, error)
, which takes a structure and returns a populated url.Values
.
By default, the key value in the URL query string is the field name of the structure. If the field does not need to be encoded, you can write url:"-"
and for scenarios where null values need to be ignored, add omitempty
, for example as follows.
Converting a structure to a query string is very simple, requiring only a Values method call to convert the structure to url.Values
, and then constructing the query string from the Encode method of `url.
Using go-querystring to rework old code
In the previous project code, I used a lot of if-determinations to add query parameters one by one, as many times as there are parameters. And I used some other methods of net/url
to finally encode the URL of the HTTP request.
|
|
With the go-querystring package, the code can be made very clean.
Summary
go-querystring is a very simple and problem-oriented library that does one thing: transform custom structs to url.Values. The problem is very focused, and the solution is very extreme, which is what every open source project needs to learn and learn from.