Menu
  • HOME
  • TAGS

golang, db *sql.DB declared in a.go not available in b.go

sql,database,go,go-gin

EDIT: As pointed out by DaveC, the problem is that using := initiates a variable in the local scope only. Declaring err beforehand will cause the sql.Open to save the connection in the global instead of creating a new local, as follows: func main() { var err error // <-...

Determine if POST data value matches struct field type

go,go-gin

Gin has a built-in validation engine: https://github.com/bluesuncorp/validator/blob/v5/baked_in.go but you can use your own or disable it completely. The validator does not validate the wire data (json string), instead it validates the binded struct: LearnMoreImage string `db:"learn_more_image" json:"learn_more_image,omitempty" binding:"required"` ApiVersion int64 `db:"api_version" json:"api_version" binding:"required,min=1"` Notice this: binding:"required,min=1" Then: err := c.Bind(&json)...