- 基础用法:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.String(200, "Index")
})
r.GET("/user/:name", func(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "Hello %s", name)
})
r.POST("/form", func(c *gin.Context) {
username := c.PostForm("username")
password := c.DefaultPostForm("password", "000000")
c.JSON(http.StatusOK, gin.H{
"username": username,
"password": password,
})
})
r.POST("/upload", func(c *gin.Context) {
file, _ := c.FormFile("file")
// c.SaveUploadedFile(file, dst)
c.String(http.StatusOK, "%s uploaded!", file.Filename)
})
r.Run("0.0.0.0:8800")
}
2. 常用方法
// c.Abort()
// c.AbortWithError(code int, err error) *Error
// c.AbortWithStatus(code int)
// c.AbortWithStatusJSON(code int, jsonObj interface})
// c.AsciiJSON(code int, obj interface})
// c.Bind(obj interface}) error
// c.BindHeader(obj interface}) error
// c.BindJSON(obj interface}) error
// c.BindQuery(obj interface}) error
// c.BindUri(obj interface}) error
// c.BindXML(obj interface}) error
// c.BindYAML(obj interface}) error
// c.ClientIP() string
// c.ContentType() string
// c.Cookie(name string) (string, error)
// c.Data(code int, contentType string, data []byte)
// c.DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string)
// c.Deadline() (deadline time.Time, ok bool)
// c.DefaultPostForm(key, defaultValue string) string
// c.DefaultQuery(key, defaultValue string) string
// c.Done() <-chan struct}
// c.Err() error
// c.Error(err error) *Error
// c.File(filepath string)
// c.FileAttachment(filepath, filename string)
// c.FileFromFS(filepath string, fs http.FileSystem)
// c.FormFile(name string) (*multipart.FileHeader, error)
// c.FullPath() string
// c.func bodyAllowedForStatus(status int) bool
// c.func validateHeader(header string) (clientIP string, valid bool)
// c.Get(key string) (value interface}, exists bool)
// c.get(m map[string][]string, key string) (map[string]string, bool)
// c.GetBool(key string) (b bool)
// c.GetDuration(key string) (d time.Duration)
// c.GetFloat64(key string) (f64 float64)
// c.GetHeader(key string) string
// c.GetInt(key string) (i int)
// c.GetInt64(key string) (i64 int64)
// c.GetPostForm(key string) (string, bool)
// c.GetPostFormArray(key string) ([]string, bool)
// c.GetPostFormMap(key string) (map[string]string, bool)
// c.GetQuery(key string) (string, bool)
// c.GetQueryArray(key string) ([]string, bool)
// c.GetQueryMap(key string) (map[string]string, bool)
// c.GetRawData() ([]byte, error)
// c.GetString(key string) (s string)
// c.GetStringMap(key string) (sm map[string]interface})
// c.GetStringMapString(key string) (sms map[string]string)
// c.GetStringMapStringSlice(key string) (smss map[string][]string)
// c.GetStringSlice(key string) (ss []string)
// c.GetTime(key string) (t time.Time)
// c.GetUint(key string) (ui uint)
// c.GetUint64(key string) (ui64 uint64)
// c.Handler() HandlerFunc
// c.HandlerName() string
// c.HandlerNames() []string
// c.Header(key, value string)
// c.HTML(code int, name string, obj interface})
// c.IndentedJSON(code int, obj interface})
// c.initFormCache()
// c.initQueryCache()
// c.IsAborted() bool
// c.IsWebsocket() bool
// c.JSON(code int, obj interface})
// c.JSONP(code int, obj interface})
// c.MultipartForm() (*multipart.Form, error)
// c.MustBindWith(obj interface}, b binding.Binding) error
// c.MustGet(key string) interface}
// c.Negotiate(code int, config Negotiate)
// c.NegotiateFormat(offered ...string) string
// c.Next()
// c.Param(key string) string
// c.PostForm(key string) string
// c.PostFormArray(key string) []string
// c.PostFormMap(key string) map[string]string
// c.ProtoBuf(code int, obj interface})
// c.PureJSON(code int, obj interface})
// c.Query(key string) string
// c.QueryArray(key string) []string
// c.QueryMap(key string) map[string]string
// c.Redirect(code int, location string)
// c.RemoteIP() (net.IP, bool)
// c.Render(code int, r render.Render)
// c.requestHeader(key string) string
// c.SaveUploadedFile(file *multipart.FileHeader, dst string) error
// c.SecureJSON(code int, obj interface})
// c.Set(key string, value interface})
// c.SetAccepted(formats ...string)
// c.SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
// c.SetSameSite(samesite http.SameSite)
// c.ShouldBind(obj interface}) error
// c.ShouldBindBodyWith(obj interface}, bb binding.BindingBody) (err error)
// c.ShouldBindHeader(obj interface}) error
// c.ShouldBindJSON(obj interface}) error
// c.ShouldBindQuery(obj interface}) error
// c.ShouldBindUri(obj interface}) error
// c.ShouldBindWith(obj interface}, b binding.Binding) error
// c.ShouldBindXML(obj interface}) error
// c.ShouldBindYAML(obj interface}) error
// c.SSEvent(name string, message interface})
// c.Status(code int)
// c.Stream(step func(w io.Writer) bool) bool
// c.String(code int, format string, values ...interface})
// c.Value(key interface}) interface}
// c.XML(code int, obj interface})
// c.YAML(code int, obj interface})