I have implemented rest api
using golang
, gin
and gorp
Employee structure:
type Employee struct {
Id int64 `db:"id" json:"id"`
Firstname string `db:"firstname" json:"firstname"`
Lastname string `db:"lastname" json:"lastname"`
Dob time.Time `db:"dob" json:"dob"`
Skills []string `db:skills json:"skills"`
}
In POST
sending request as:
func PostEmployee(c *gin.Context) {
var emp Employee
c.Bind(&emp)
skills, _ := json.Marshal(emp.Skills)
if emp.Firstname != "" && emp.Lastname != "" {
if insert, _ := dbmap.Exec(`INSERT INTO employee (firstname, lastname, dob, skills) VALUES (?, ?, ?, ?)`, emp.Firstname, emp.Lastname, emp.Dob, skills); insert != nil {
emp_id, err := insert.LastInsertId()
.....
}
......
}
This save data to mysql
database, works perfect.
For retrieving data from database implemented GET
request
func GetEmployees(c *gin.Context) {
var emps []Employee
_, err := dbmap.Select(&emps, "SELECT * FROM employee")
log.Println(err)
if err == nil {
c.JSON(200, emps)
} else {
c.JSON(404, gin.H{"error": "no employee(s) into the table"})
}
GET
query doesn't gives any data from database and log.Println(err)
log says:
Scan error on column index 4: unsupported Scan, storing driver.Value type []uint8 into type *[]string
Any ideas?
Faced similar issue, for me problem was sequencing of field "scope_t".
When inserting the data I placed "scope_t" at a random place so SQL return with the error mentioned above reason being that position was mapped with a different data type.
TL;DR
Before inserting also check the places you might be interchanging the values due to which type error is caused.