I have a Postgres DB which has the following entry
I'm using GIN and have the following in my main function
func main() {
router := gin.Default()
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", env_var.Host, env_var.User, env_var.Password, env_var.DBname, env_var.Port, env_var.SSLMODE, env_var.TimeZone)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to db")
}
router.GET("/source/:id", han.GetSource(db))
router.Run("localhost:8080")
}
Here are the structs and handlers
func GetSource(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
var source models.Source
db.Where("id = ?", id).Find(&source)
c.JSON(http.StatusOK, source)
}
}
type Source struct {
ID int `json:"id"`
Source_Name string `json:"source_name"`
Abrviation string `json:"abrviation"`
Last_Modified time.Time `json:"last_modified"`
}
func (Source) TableName() string {
return "artwork_migrate_source"
}
When I use the following curl command
curl localhost:8080/source/1
I get the following response:
{"id":1,"source_name":"Rijksmuseum","abrviation":"","last_modified":"2022-08-05T00:00:00Z"}%
So far so good, but if you notice, the "abbreviation" field is empty. The field in my db is of type varchar with a limit of 3.
CodePudding user response:
Simple mistake, I misspelled the "Abriviation" field in the struct.
Thanks to @Brits