github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_request_param_param.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package ghttp 8 9 import "github.com/wangyougui/gf/v2/container/gvar" 10 11 // SetParam sets custom parameter with key-value pairs. 12 func (r *Request) SetParam(key string, value interface{}) { 13 if r.paramsMap == nil { 14 r.paramsMap = make(map[string]interface{}) 15 } 16 r.paramsMap[key] = value 17 } 18 19 // SetParamMap sets custom parameter with key-value pair maps. 20 func (r *Request) SetParamMap(data map[string]interface{}) { 21 if r.paramsMap == nil { 22 r.paramsMap = make(map[string]interface{}) 23 } 24 for k, v := range data { 25 r.paramsMap[k] = v 26 } 27 } 28 29 // GetParam returns custom parameter with a given name `key`. 30 // It returns `def` if `key` does not exist. 31 // It returns nil if `def` is not passed. 32 func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var { 33 if len(r.paramsMap) > 0 { 34 if value, ok := r.paramsMap[key]; ok { 35 return gvar.New(value) 36 } 37 } 38 if len(def) > 0 { 39 return gvar.New(def[0]) 40 } 41 return nil 42 }