'How to update(C.R.U.D) the original slice value in Golang with PostgreSQL
I am just getting started learning Golang and PostgreSQL. I tried to make a simple Todo_List to improve my coding skill.
For now, I try to code the "Update" part in C.R.U.D. I received and read data from http.Reqeust by using Unmarshal and when I check the console it printed out as well. However, I am struggling with updating this receiving data to an original slice. (In my code, todo)
Could you help me which part is wrong or how to solve it??
Here is my whole code: https://go.dev/play/p/qjW74UVEjpR
I am sorry there are pretty many comments and fmt.Println. I tried to check all conditions what I want...
Solution 1:[1]
you should use Updates method. Link
var params = map[string]interface{}{}
if todoDetails.Id != 0 {
todoDetails.Id = updateTodo.Id
}
if todoDetails.Title != "" {
//todoDetails.Title = updateTodo.Title
params["title"] = todoDetails.Title
}
if todoDetails.Description != "" {
//todoDetails.Description = updateTodo.Description
params["description"] = todoDetails.Description
}
if todoDetails.Condition != true || false {
//todoDetails.Condition = updateTodo.Condition
params["condition"] = todoDetails.Condition
}
err = db.Model(&Todo{}).Where("id = ?",updateTodo.Id).Updates(params).Error
if err != nil {
fmt.Println(err)
return
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ahmetlutfu |
