1. 程式人生 > 其它 >Go gin Get請求引數 支援陣列

Go gin Get請求引數 支援陣列

技術標籤:golanggolangginGetslice陣列

思路:用string接收,然後通過反序列化成陣列即可。

controller/service:

// str: [1,2]
int8Slice := utils.ParamInt8Slice(str)

// str: ["[email protected]", "[email protected]"]
stringSlice := utils.ParamStringSlice(str)

utils:


import (
	jsoniter "github.com/json-iterator/go"
) var json = jsoniter.ConfigCompatibleWithStandardLibrary func ParamInt8Slice(str string) []int8 { res := make([]int8, 0) if IsEmptyString(str) { return res } if err := Deserialize(str, &res); err != nil { log.Warnf("Error to deserialize string of []int8:%s, err=%v", str, err)
return res } return res } func ParamInt64Slice(str string) []int64 { res := make([]int64, 0) if IsEmptyString(str) { return res } if err := Deserialize(str, &res); err != nil { log.Warnf("Error to deserialize string of []int64:%s, err=%v", str, err) return res } return
res } func ParamStringSlice(str string) []string { res := make([]string, 0) if IsEmptyString(str) { return res } if err := Deserialize(str, &res); err != nil { log.Warnf("Error to deserialize string of []string:%s, err=%v", str, err) return res } return res } func Deserialize(s string, obj interface{}) error { return json.UnmarshalFromString(s, obj) }