Files
gf/net/ghttp/ghttp_request_param_param.go

31 lines
880 B
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
2021-10-11 21:41:56 +08:00
import "github.com/gogf/gf/v2/container/gvar"
// SetParam sets custom parameter with key-value pair.
func (r *Request) SetParam(key string, value interface{}) {
2019-12-01 14:07:36 +08:00
if r.paramsMap == nil {
r.paramsMap = make(map[string]interface{})
2019-06-19 09:06:52 +08:00
}
2019-12-01 14:07:36 +08:00
r.paramsMap[key] = value
}
// GetParam returns custom parameter with given name `key`.
// It returns `def` if `key` does not exist.
// It returns nil if `def` is not passed.
2021-09-27 21:27:24 +08:00
func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var {
2019-12-01 14:07:36 +08:00
if r.paramsMap != nil {
2021-09-27 21:27:24 +08:00
return gvar.New(r.paramsMap[key])
2019-06-19 09:06:52 +08:00
}
if len(def) > 0 {
2021-09-27 21:27:24 +08:00
return gvar.New(def[0])
2019-06-19 09:06:52 +08:00
}
return nil
}