diff options
| author | Shulhan <ms@kilabit.info> | 2021-04-09 19:41:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-04-09 19:41:29 +0700 |
| commit | 9d741226536b21e53b0ac78d7585539af67a2398 (patch) | |
| tree | 6edf3788ab8cd82fb0f18a67f8cfc07255145170 | |
| parent | eb2dbaf27910f968f21c178929d056b64e9f242c (diff) | |
| download | gorankusu-9d741226536b21e53b0ac78d7585539af67a2398.tar.xz | |
all: return non-nil on ToHttpHeader, ToMultipartForm, and ToUrlValues
The returned value can be set by the caller later, so to minimize
checking for nil and recreate the instance, initialize the value in
the function and return it even if its empty.
| -rw-r--r-- | key_value.go | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/key_value.go b/key_value.go index 91274b6..2001ca5 100644 --- a/key_value.go +++ b/key_value.go @@ -14,11 +14,11 @@ type KeyValue map[string]string // // ToHttpHeader convert the KeyValue to the standard http.Header. // -func (kv KeyValue) ToHttpHeader() http.Header { +func (kv KeyValue) ToHttpHeader() (headers http.Header) { + headers = http.Header{} if kv == nil || len(kv) == 0 { - return nil + return headers } - headers := http.Header{} for k, v := range kv { headers.Set(k, v) } @@ -29,11 +29,10 @@ func (kv KeyValue) ToHttpHeader() http.Header { // ToMultipartFormData convert the KeyValue into map of string and raw bytes. // func (kv KeyValue) ToMultipartFormData() (data map[string][]byte) { + data = make(map[string][]byte, len(kv)) if kv == nil || len(kv) == 0 { - return nil + return data } - - data = make(map[string][]byte, len(kv)) for k, v := range kv { data[k] = []byte(v) } @@ -43,11 +42,11 @@ func (kv KeyValue) ToMultipartFormData() (data map[string][]byte) { // // ToUrlValues convert the KeyValue to the standard url.Values. // -func (kv KeyValue) ToUrlValues() url.Values { +func (kv KeyValue) ToUrlValues() (vals url.Values) { + vals = url.Values{} if kv == nil || len(kv) == 0 { - return nil + return vals } - vals := url.Values{} for k, v := range kv { vals.Set(k, v) } |
