Documentation
¶
Overview ¶
Package gin implements a HTTP web framework called gin.
See https://gin-gonic.com/ for more information about gin.
Example:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
Index ¶
- Constants
- Variables
- func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine)
- func Dir(root string, listDirectory bool) http.FileSystem
- func DisableBindValidation()
- func DisableConsoleColor()
- func EnableJsonDecoderDisallowUnknownFields()
- func EnableJsonDecoderUseNumber()
- func ForceConsoleColor()
- func IsDebugging() bool
- func Mode() string
- func SetMode(value string)
- type Accounts
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(code int, err error) *Error
- func (c *Context) AbortWithStatus(code int)
- func (c *Context) AbortWithStatusJSON(code int, jsonObj any)
- func (c *Context) AbortWithStatusPureJSON(code int, jsonObj any)
- func (c *Context) AddParam(key, value string)
- func (c *Context) AsciiJSON(code int, obj any)
- func (c *Context) BSON(code int, obj any)
- func (c *Context) Bind(obj any) error
- func (c *Context) BindHeader(obj any) error
- func (c *Context) BindJSON(obj any) error
- func (c *Context) BindPlain(obj any) error
- func (c *Context) BindQuery(obj any) error
- func (c *Context) BindTOML(obj any) error
- func (c *Context) BindUri(obj any) error
- func (c *Context) BindWith(obj any, b binding.Binding) errordeprecated
- func (c *Context) BindXML(obj any) error
- func (c *Context) BindYAML(obj any) error
- func (c *Context) ClientIP() string
- func (c *Context) ContentType() string
- func (c *Context) Cookie(name string) (string, error)
- func (c *Context) Copy() *Context
- func (c *Context) Data(code int, contentType string, data []byte)
- func (c *Context) DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, ...)
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) DefaultPostForm(key, defaultValue string) string
- func (c *Context) DefaultQuery(key, defaultValue string) string
- func (c *Context) Delete(key any)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Error(err error) *Error
- func (c *Context) File(filepath string)
- func (c *Context) FileAttachment(filepath, filename string)
- func (c *Context) FileFromFS(filepath string, fs http.FileSystem)
- func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) FullPath() string
- func (c *Context) Get(key any) (value any, exists bool)
- func (c *Context) GetBool(key any) bool
- func (c *Context) GetDuration(key any) time.Duration
- func (c *Context) GetError(key any) error
- func (c *Context) GetErrorSlice(key any) []error
- func (c *Context) GetFloat32(key any) float32
- func (c *Context) GetFloat32Slice(key any) []float32
- func (c *Context) GetFloat64(key any) float64
- func (c *Context) GetFloat64Slice(key any) []float64
- func (c *Context) GetHeader(key string) string
- func (c *Context) GetInt(key any) int
- func (c *Context) GetInt8(key any) int8
- func (c *Context) GetInt8Slice(key any) []int8
- func (c *Context) GetInt16(key any) int16
- func (c *Context) GetInt16Slice(key any) []int16
- func (c *Context) GetInt32(key any) int32
- func (c *Context) GetInt32Slice(key any) []int32
- func (c *Context) GetInt64(key any) int64
- func (c *Context) GetInt64Slice(key any) []int64
- func (c *Context) GetIntSlice(key any) []int
- func (c *Context) GetPostForm(key string) (string, bool)
- func (c *Context) GetPostFormArray(key string) (values []string, ok bool)
- func (c *Context) GetPostFormMap(key string) (map[string]string, bool)
- func (c *Context) GetQuery(key string) (string, bool)
- func (c *Context) GetQueryArray(key string) (values []string, ok bool)
- func (c *Context) GetQueryMap(key string) (map[string]string, bool)
- func (c *Context) GetRawData() ([]byte, error)
- func (c *Context) GetString(key any) string
- func (c *Context) GetStringMap(key any) map[string]any
- func (c *Context) GetStringMapString(key any) map[string]string
- func (c *Context) GetStringMapStringSlice(key any) map[string][]string
- func (c *Context) GetStringSlice(key any) []string
- func (c *Context) GetTime(key any) time.Time
- func (c *Context) GetUint(key any) uint
- func (c *Context) GetUint8(key any) uint8
- func (c *Context) GetUint8Slice(key any) []uint8
- func (c *Context) GetUint16(key any) uint16
- func (c *Context) GetUint16Slice(key any) []uint16
- func (c *Context) GetUint32(key any) uint32
- func (c *Context) GetUint32Slice(key any) []uint32
- func (c *Context) GetUint64(key any) uint64
- func (c *Context) GetUint64Slice(key any) []uint64
- func (c *Context) GetUintSlice(key any) []uint
- func (c *Context) HTML(code int, name string, obj any)
- func (c *Context) Handler() HandlerFunc
- func (c *Context) HandlerName() string
- func (c *Context) HandlerNames() []string
- func (c *Context) Header(key, value string)
- func (c *Context) IndentedJSON(code int, obj any)
- func (c *Context) IsAborted() bool
- func (c *Context) IsWebsocket() bool
- func (c *Context) JSON(code int, obj any)
- func (c *Context) JSONP(code int, obj any)
- func (c *Context) MultipartForm() (*multipart.Form, error)
- func (c *Context) MustBindWith(obj any, b binding.Binding) error
- func (c *Context) MustGet(key any) any
- func (c *Context) Negotiate(code int, config Negotiate)
- func (c *Context) NegotiateFormat(offered ...string) string
- func (c *Context) Next()
- func (c *Context) Param(key string) string
- func (c *Context) PostForm(key string) (value string)
- func (c *Context) PostFormArray(key string) (values []string)
- func (c *Context) PostFormMap(key string) (dicts map[string]string)
- func (c *Context) ProtoBuf(code int, obj any)
- func (c *Context) PureJSON(code int, obj any)
- func (c *Context) Query(key string) (value string)
- func (c *Context) QueryArray(key string) (values []string)
- func (c *Context) QueryMap(key string) (dicts map[string]string)
- func (c *Context) Redirect(code int, location string)
- func (c *Context) RemoteIP() string
- func (c *Context) Render(code int, r render.Render)
- func (c *Context) SSEvent(name string, message any)
- func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string, perm ...fs.FileMode) error
- func (c *Context) SecureJSON(code int, obj any)
- func (c *Context) Set(key any, value any)
- func (c *Context) SetAccepted(formats ...string)
- func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
- func (c *Context) SetCookieData(cookie *http.Cookie)
- func (c *Context) SetSameSite(samesite http.SameSite)
- func (c *Context) ShouldBind(obj any) error
- func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error)
- func (c *Context) ShouldBindBodyWithJSON(obj any) error
- func (c *Context) ShouldBindBodyWithPlain(obj any) error
- func (c *Context) ShouldBindBodyWithTOML(obj any) error
- func (c *Context) ShouldBindBodyWithXML(obj any) error
- func (c *Context) ShouldBindBodyWithYAML(obj any) error
- func (c *Context) ShouldBindHeader(obj any) error
- func (c *Context) ShouldBindJSON(obj any) error
- func (c *Context) ShouldBindPlain(obj any) error
- func (c *Context) ShouldBindQuery(obj any) error
- func (c *Context) ShouldBindTOML(obj any) error
- func (c *Context) ShouldBindUri(obj any) error
- func (c *Context) ShouldBindWith(obj any, b binding.Binding) error
- func (c *Context) ShouldBindXML(obj any) error
- func (c *Context) ShouldBindYAML(obj any) error
- func (c *Context) Status(code int)
- func (c *Context) Stream(step func(w io.Writer) bool) bool
- func (c *Context) String(code int, format string, values ...any)
- func (c *Context) TOML(code int, obj any)
- func (c *Context) Value(key any) any
- func (c *Context) XML(code int, obj any)
- func (c *Context) YAML(code int, obj any)
- type ContextKeyType
- type Engine
- func (engine *Engine) Delims(left, right string) *Engine
- func (engine *Engine) HandleContext(c *Context)
- func (engine *Engine) Handler() http.Handler
- func (engine *Engine) LoadHTMLFS(fs http.FileSystem, patterns ...string)
- func (engine *Engine) LoadHTMLFiles(files ...string)
- func (engine *Engine) LoadHTMLGlob(pattern string)
- func (engine *Engine) NoMethod(handlers ...HandlerFunc)
- func (engine *Engine) NoRoute(handlers ...HandlerFunc)
- func (engine *Engine) Routes() (routes RoutesInfo)
- func (engine *Engine) Run(addr ...string) (err error)
- func (engine *Engine) RunFd(fd int) (err error)
- func (engine *Engine) RunListener(listener net.Listener) (err error)
- func (engine *Engine) RunQUIC(addr, certFile, keyFile string) (err error)
- func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error)
- func (engine *Engine) RunUnix(file string) (err error)
- func (engine *Engine) SecureJsonPrefix(prefix string) *Engine
- func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (engine *Engine) SetFuncMap(funcMap template.FuncMap)
- func (engine *Engine) SetHTMLTemplate(templ *template.Template)
- func (engine *Engine) SetTrustedProxies(trustedProxies []string) error
- func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes
- func (engine *Engine) With(opts ...OptionFunc) *Engine
- type Error
- type ErrorType
- type H
- type HandlerFunc
- func BasicAuth(accounts Accounts) HandlerFunc
- func BasicAuthForProxy(accounts Accounts, realm string) HandlerFunc
- func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc
- func Bind(val any) HandlerFunc
- func CustomRecovery(handle RecoveryFunc) HandlerFunc
- func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc
- func ErrorLogger() HandlerFunc
- func ErrorLoggerT(typ ErrorType) HandlerFunc
- func Logger() HandlerFunc
- func LoggerWithConfig(conf LoggerConfig) HandlerFunc
- func LoggerWithFormatter(f LogFormatter) HandlerFunc
- func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc
- func Recovery() HandlerFunc
- func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc
- func WrapF(f http.HandlerFunc) HandlerFunc
- func WrapH(h http.Handler) HandlerFunc
- type HandlersChain
- type IRouter
- type IRoutes
- type LogFormatter
- type LogFormatterParams
- type LoggerConfig
- type Negotiate
- type OnlyFilesFS
- type OptionFunc
- type Param
- type Params
- type RecoveryFunc
- type ResponseWriter
- type RouteInfo
- type RouterGroup
- func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) BasePath() string
- func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup
- func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) Match(methods []string, relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes
- func (group *RouterGroup) Static(relativePath, root string) IRoutes
- func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes
- func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes
- func (group *RouterGroup) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes
- func (group *RouterGroup) Use(middleware ...HandlerFunc) IRoutes
- type RoutesInfo
- type Skipper
Constants ¶
const ( MIMEJSON = binding.MIMEJSON MIMEHTML = binding.MIMEHTML MIMEXML = binding.MIMEXML MIMEXML2 = binding.MIMEXML2 MIMEPlain = binding.MIMEPlain MIMEPOSTForm = binding.MIMEPOSTForm MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm MIMEYAML = binding.MIMEYAML MIMEYAML2 = binding.MIMEYAML2 MIMETOML = binding.MIMETOML MIMEPROTOBUF = binding.MIMEPROTOBUF MIMEBSON = binding.MIMEBSON )
Content-Type MIME of the most common data formats.
const ( // PlatformGoogleAppEngine when running on Google App Engine. Trust X-Appengine-Remote-Addr // for determining the client's IP PlatformGoogleAppEngine = "X-Appengine-Remote-Addr" // PlatformCloudflare when using Cloudflare's CDN. Trust CF-Connecting-IP for determining // the client's IP PlatformCloudflare = "CF-Connecting-IP" // PlatformFlyIO when running on Fly.io. Trust Fly-Client-IP for determining the client's IP PlatformFlyIO = "Fly-Client-IP" )
Trusted platforms
const ( // DebugMode indicates gin mode is debug. DebugMode = "debug" // ReleaseMode indicates gin mode is release. ReleaseMode = "release" // TestMode indicates gin mode is test. TestMode = "test" )
const AuthProxyUserKey = "proxy_user"
AuthProxyUserKey is the cookie name for proxy_user credential in basic auth for proxy.
const AuthUserKey = "user"
AuthUserKey is the cookie name for user credential in basic auth.
const BindKey = "_gin-gonic/gin/bindkey"
BindKey indicates a default bind key.
const BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
BodyBytesKey indicates a default body bytes key.
const ContextKey = "_gin-gonic/gin/contextkey"
ContextKey is the key that a Context returns itself for.
const EnvGinMode = "GIN_MODE"
EnvGinMode indicates environment name for gin mode.
const Version = "v1.12.0"
Version is the current gin framework's version.
Variables ¶
var DebugPrintFunc func(format string, values ...any)
DebugPrintFunc indicates debug log output format.
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
DebugPrintRouteFunc indicates debug log output format.
var DefaultErrorWriter io.Writer = os.Stderr
DefaultErrorWriter is the default io.Writer used by Gin to debug errors
var DefaultWriter io.Writer = os.Stdout
DefaultWriter is the default io.Writer used by Gin for debug output and middleware output like Logger() or Recovery(). Note that both Logger and Recovery provides custom ways to configure their output io.Writer. To support coloring in Windows use:
import "github.com/mattn/go-colorable" gin.DefaultWriter = colorable.NewColorableStdout()
Functions ¶
func CreateTestContext ¶ added in v1.3.0
func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine)
CreateTestContext returns a fresh Engine and a Context associated with it. This is useful for tests that need to set up a new Gin engine instance along with a context, for example, to test middleware that doesn't depend on specific routes. The ResponseWriter `w` is used to initialize the context's writer.
func Dir ¶
func Dir(root string, listDirectory bool) http.FileSystem
Dir returns an http.FileSystem that can be used by http.FileServer(). It is used internally in router.Static(). if listDirectory == true, then it works the same as http.Dir(), otherwise it returns a filesystem that prevents http.FileServer() to list the directory files.
func DisableBindValidation ¶
func DisableBindValidation()
DisableBindValidation closes the default validator.
func DisableConsoleColor ¶ added in v1.3.0
func DisableConsoleColor()
DisableConsoleColor disables color output in the console.
func EnableJsonDecoderDisallowUnknownFields ¶ added in v1.5.0
func EnableJsonDecoderDisallowUnknownFields()
EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to call the DisallowUnknownFields method on the JSON Decoder instance.
func EnableJsonDecoderUseNumber ¶ added in v1.3.0
func EnableJsonDecoderUseNumber()
EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to call the UseNumber method on the JSON Decoder instance.
func ForceConsoleColor ¶ added in v1.4.0
func ForceConsoleColor()
ForceConsoleColor force color output in the console.
func IsDebugging ¶
func IsDebugging() bool
IsDebugging returns true if the framework is running in debug mode. Use SetMode(gin.ReleaseMode) to disable debug mode.
Types ¶
type Context ¶
type Context struct {
Request *http.Request
Writer ResponseWriter
Params Params
// Keys is a key/value pair exclusively for the context of each request.
Keys map[any]any
// Errors is a list of errors attached to all the handlers/middlewares who used this context.
Errors errorMsgs
// Accepted defines a list of manually accepted formats for content negotiation.
Accepted []string
// contains filtered or unexported fields
}
Context is the most important part of gin. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.
func CreateTestContextOnly ¶ added in v1.8.2
func CreateTestContextOnly(w http.ResponseWriter, r *Engine) (c *Context)
CreateTestContextOnly returns a fresh Context associated with the provided Engine `r`. This is useful for tests that operate on an existing, possibly pre-configured, Gin engine instance and need a new context for it. The ResponseWriter `w` is used to initialize the context's writer. The context is allocated with the `maxParams` setting from the provided engine.
func (*Context) Abort ¶
func (c *Context) Abort()
Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.
func (*Context) AbortWithError ¶
AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and pushes the specified error to `c.Errors`. See Context.Error() for more details.
func (*Context) AbortWithStatus ¶
AbortWithStatus calls `Abort()` and writes the headers with the specified status code. For example, a failed attempt to authenticate a request could use: context.AbortWithStatus(401).
func (*Context) AbortWithStatusJSON ¶ added in v1.3.0
AbortWithStatusJSON calls `Abort()` and then `JSON` internally. This method stops the chain, writes the status code and return a JSON body. It also sets the Content-Type as "application/json".
func (*Context) AbortWithStatusPureJSON ¶ added in v1.11.0
AbortWithStatusPureJSON calls `Abort()` and then `PureJSON` internally. This method stops the chain, writes the status code and return a JSON body without escaping. It also sets the Content-Type as "application/json".
func (*Context) AddParam ¶ added in v1.8.0
AddParam adds param to context and replaces path param key with given value for e2e testing purposes Example Route: "/user/:id" AddParam("id", 1) Result: "/user/1"
func (*Context) AsciiJSON ¶ added in v1.3.0
AsciiJSON serializes the given struct as JSON into the response body with unicode to ASCII string. It also sets the Content-Type as "application/json".
func (*Context) BSON ¶ added in v1.12.0
BSON serializes the given struct as BSON into the response body.
func (*Context) Bind ¶
Bind checks the Method and Content-Type to select a binding engine automatically, Depending on the "Content-Type" header different bindings are used, for example:
"application/json" --> JSON binding "application/xml" --> XML binding
It parses the request's body based on the Content-Type (e.g., JSON or XML). It decodes the payload into the struct specified as a pointer. It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (*Context) BindHeader ¶ added in v1.5.0
BindHeader is a shortcut for c.MustBindWith(obj, binding.Header).
func (*Context) BindPlain ¶ added in v1.11.0
BindPlain is a shortcut for c.MustBindWith(obj, binding.Plain).
func (*Context) BindQuery ¶ added in v1.3.0
BindQuery is a shortcut for c.MustBindWith(obj, binding.Query).
func (*Context) BindTOML ¶ added in v1.8.0
BindTOML is a shortcut for c.MustBindWith(obj, binding.TOML).
func (*Context) BindUri ¶ added in v1.4.0
BindUri binds the passed struct pointer using binding.Uri. It will abort the request with HTTP 400 if any error occurs.
func (*Context) BindXML ¶ added in v1.4.0
BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).
func (*Context) BindYAML ¶ added in v1.4.0
BindYAML is a shortcut for c.MustBindWith(obj, binding.YAML).
func (*Context) ClientIP ¶
ClientIP implements one best effort algorithm to return the real client IP. It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not. If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-IP]). If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, the remote IP (coming from Request.RemoteAddr) is returned.
func (*Context) ContentType ¶
ContentType returns the Content-Type header of the request.
func (*Context) Cookie ¶
Cookie returns the named cookie provided in the request or ErrNoCookie if not found. And return the named cookie is unescaped. If multiple cookies match the given name, only one cookie will be returned.
func (*Context) Copy ¶
Copy returns a copy of the current context that can be safely used outside the request's scope. This has to be used when the context has to be passed to a goroutine.
func (*Context) DataFromReader ¶ added in v1.3.0
func (c *Context) DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string)
DataFromReader writes the specified reader into the body stream and updates the HTTP code.
func (*Context) Deadline ¶
Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
func (*Context) DefaultPostForm ¶
DefaultPostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns the specified defaultValue string. See: PostForm() and GetPostForm() for further information.
func (*Context) DefaultQuery ¶
DefaultQuery returns the keyed url query value if it exists, otherwise it returns the specified defaultValue string. See: Query() and GetQuery() for further information.
GET /?name=Manu&lastname=
c.DefaultQuery("name", "unknown") == "Manu"
c.DefaultQuery("id", "none") == "none"
c.DefaultQuery("lastname", "none") == ""
func (*Context) Delete ¶ added in v1.12.0
Delete deletes the key from the Context's Key map, if it exists. This operation is safe to be used by concurrent go-routines
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done returns nil (chan which will wait forever) when c.Request has no Context.
func (*Context) Error ¶
Error attaches an error to the current context. The error is pushed to a list of errors. It's a good idea to call Error for each error that occurred during the resolution of a request. A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response. Error will panic if err is nil.
func (*Context) FileAttachment ¶ added in v1.4.0
FileAttachment writes the specified file into the body stream in an efficient way On the client side, the file will typically be downloaded with the given filename
func (*Context) FileFromFS ¶ added in v1.6.0
func (c *Context) FileFromFS(filepath string, fs http.FileSystem)
FileFromFS writes the specified file from http.FileSystem into the body stream in an efficient way.
func (*Context) FormFile ¶ added in v1.3.0
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
FormFile returns the first file for the provided form key.
func (*Context) FullPath ¶ added in v1.5.0
FullPath returns a matched route full path. For not found routes returns an empty string.
router.GET("/user/:id", func(c *gin.Context) {
c.FullPath() == "/user/:id" // true
})
func (*Context) Get ¶
Get returns the value for the given key, ie: (value, true). If the value does not exist it returns (nil, false)
func (*Context) GetBool ¶ added in v1.3.0
GetBool returns the value associated with the key as a boolean.
func (*Context) GetDuration ¶ added in v1.3.0
GetDuration returns the value associated with the key as a duration.
func (*Context) GetError ¶ added in v1.12.0
GetError returns the value associated with the key as an error.
func (*Context) GetErrorSlice ¶ added in v1.12.0
GetErrorSlice returns the value associated with the key as a slice of errors.
func (*Context) GetFloat32 ¶ added in v1.11.0
GetFloat32 returns the value associated with the key as a float32.
func (*Context) GetFloat32Slice ¶ added in v1.11.0
GetFloat32Slice returns the value associated with the key as a slice of float32 numbers.
func (*Context) GetFloat64 ¶ added in v1.3.0
GetFloat64 returns the value associated with the key as a float64.
func (*Context) GetFloat64Slice ¶ added in v1.11.0
GetFloat64Slice returns the value associated with the key as a slice of float64 numbers.
func (*Context) GetInt ¶ added in v1.3.0
GetInt returns the value associated with the key as an integer.
func (*Context) GetInt8 ¶ added in v1.11.0
GetInt8 returns the value associated with the key as an integer 8.
func (*Context) GetInt8Slice ¶ added in v1.11.0
GetInt8Slice returns the value associated with the key as a slice of int8 integers.
func (*Context) GetInt16 ¶ added in v1.11.0
GetInt16 returns the value associated with the key as an integer 16.
func (*Context) GetInt16Slice ¶ added in v1.11.0
GetInt16Slice returns the value associated with the key as a slice of int16 integers.
func (*Context) GetInt32 ¶ added in v1.11.0
GetInt32 returns the value associated with the key as an integer 32.
func (*Context) GetInt32Slice ¶ added in v1.11.0
GetInt32Slice returns the value associated with the key as a slice of int32 integers.
func (*Context) GetInt64 ¶ added in v1.3.0
GetInt64 returns the value associated with the key as an integer 64.
func (*Context) GetInt64Slice ¶ added in v1.11.0
GetInt64Slice returns the value associated with the key as a slice of int64 integers.
func (*Context) GetIntSlice ¶ added in v1.11.0
GetIntSlice returns the value associated with the key as a slice of integers.
func (*Context) GetPostForm ¶
GetPostForm is like PostForm(key). It returns the specified key from a POST urlencoded form or multipart form when it exists `(value, true)` (even when the value is an empty string), otherwise it returns ("", false). For example, during a PATCH request to update the user's email:
email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"
email= --> ("", true) := GetPostForm("email") // set email to ""
--> ("", false) := GetPostForm("email") // do nothing with email
func (*Context) GetPostFormArray ¶
GetPostFormArray returns a slice of strings for a given form key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetPostFormMap ¶ added in v1.3.0
GetPostFormMap returns a map for a given form key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetQuery ¶
GetQuery is like Query(), it returns the keyed url query value if it exists `(value, true)` (even when the value is an empty string), otherwise it returns `("", false)`. It is shortcut for `c.Request.URL.Query().Get(key)`
GET /?name=Manu&lastname=
("Manu", true) == c.GetQuery("name")
("", false) == c.GetQuery("id")
("", true) == c.GetQuery("lastname")
func (*Context) GetQueryArray ¶
GetQueryArray returns a slice of strings for a given query key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetQueryMap ¶ added in v1.3.0
GetQueryMap returns a map for a given query key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetRawData ¶ added in v1.3.0
GetRawData returns stream data.
func (*Context) GetString ¶ added in v1.3.0
GetString returns the value associated with the key as a string.
func (*Context) GetStringMap ¶ added in v1.3.0
GetStringMap returns the value associated with the key as a map of interfaces.
func (*Context) GetStringMapString ¶ added in v1.3.0
GetStringMapString returns the value associated with the key as a map of strings.