github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/map.go (about) 1 package util 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "sort" 7 "strings" 8 ) 9 10 // MapBuildQuery map => url query 11 func MapBuildQuery(m map[string]any) string { 12 keys := make([]string, 0, len(m)) 13 for k := range m { 14 keys = append(keys, k) 15 } 16 sort.Strings(keys) 17 18 var params []string 19 for _, k := range keys { 20 switch v := m[k].(type) { 21 case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool, string: 22 params = append(params, fmt.Sprintf("%s=%v", k, v)) 23 default: 24 j, _ := json.Marshal(v) 25 params = append(params, fmt.Sprintf("%s=%v", k, string(j))) 26 } 27 } 28 return strings.Join(params, "&") 29 }