github.com/gogf/gf@v1.16.9/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/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import "github.com/gogf/gf/container/gvar"
    10  
    11  // SetParam sets custom parameter with key-value pair.
    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  // GetParam returns custom parameter with given name <key>.
    20  // It returns <def> if <key> does not exist.
    21  // It returns nil if <def> is not passed.
    22  func (r *Request) GetParam(key string, def ...interface{}) interface{} {
    23  	if r.paramsMap != nil {
    24  		return r.paramsMap[key]
    25  	}
    26  	if len(def) > 0 {
    27  		return def[0]
    28  	}
    29  	return nil
    30  }
    31  
    32  // GetParamVar returns custom parameter with given name <key> as gvar.Var.
    33  // It returns <def> if <key> does not exist.
    34  // It returns nil if <def> is not passed.
    35  func (r *Request) GetParamVar(key string, def ...interface{}) *gvar.Var {
    36  	return gvar.New(r.GetParam(key, def...))
    37  }