本文整理匯總了Golang中github.com/yogipriyo/Simple-Gin-Gonic-API/Godeps/_workspace/src/github.com/gin-gonic/gin.Context.Bind方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Bind方法的具體用法?Golang Context.Bind怎麽用?Golang Context.Bind使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/yogipriyo/Simple-Gin-Gonic-API/Godeps/_workspace/src/github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Bind方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UpdateUser
func UpdateUser(c *gin.Context) {
id := c.Params.ByName("id")
var user User
err := dbmap.SelectOne(&user, "SELECT * FROM user2 WHERE id=?", id)
if err == nil {
var json User
c.Bind(&json)
user_id, _ := strconv.ParseInt(id, 0, 64)
user := User{
Id: user_id,
Firstname: json.Firstname,
Lastname: json.Lastname,
}
if user.Firstname != "" && user.Lastname != "" {
_, err = dbmap.Update(&user)
if err == nil {
c.JSON(200, user)
} else {
checkErr(err, "Updated failed")
}
} else {
c.JSON(422, gin.H{"error": "fields are empty"})
}
} else {
c.JSON(404, gin.H{"error": "user not found"})
}
// curl -i -X PUT -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Merlyn\" }" http://localhost:8080/api/v1/users/1
}
示例2: PostUser
func PostUser(c *gin.Context) {
var params User
c.Bind(¶ms)
if params.Firstname != "" && params.Lastname != "" {
user := &User{0, params.Firstname, params.Lastname}
err := dbmap.Insert(user)
if err == nil {
content := &User{
Id: user.Id,
Firstname: user.Firstname,
Lastname: user.Lastname,
}
c.JSON(201, content)
} else {
c.JSON(422, gin.H{"error": err})
}
} else {
c.JSON(422, gin.H{"error": "fields are empty"})
}
// curl -i -X POST -H "Content-Type: application/json" -d "{ \"firstname\": \"Thea\", \"lastname\": \"Queen\" }" http://localhost:8080/api/v1/users
}