Solve the Error Content in Gin

Recent Observations

  1. Hyperledger Fabric Certified Practitioner (HFCP) certification appointments are open, so interested parties can take a look. https://training.linuxfoundation.cn/certificates/32

  2. On August 15, 2023, Kubernetes 1.28 was updated, which is really powerful. 1.24 entered maintenance mode on 2023-05-28 and ended its life cycle on 2023-07-28. 1.23 has stopped support on February 28.

  3. Alibaba Cloud launched Live Portait, a digital human video generation tool, to generate a digital human video by uploading a photo and a text or voice. This feature can be applied to scenarios such as live video, chatbots, and enterprise marketing. At present, the tool has been opened for experience in the Modai Community Creative Space.

  4. The iFLYTEK Spark model has been opened for testing, you can apply to try it.

Solve the error content in gin:

sql: Scan error on column index 3, name "created_at": unsupported Scan, storing driver.Value type []uint8 into type *time.Time; sql: Scan error on column index 3, name "created_at": unsupported Scan, storing driver.Value type []uint8 into type *time.Time; sql: Scan error on column index 3, name "created_at": unsupported Scan, storing driver.Value type [ ]uint8 into type *time.Time; sql: Scan error on column index 3, name "created_at": unsupported Scan, storing driver.Value type []uint8 into type *time.Time

Return data requested by the backend: "created_at": "0001-01-01T00:00:00Z"

Solution: Add the database connection setting parseTime=true, you can: dsn := "root: 123456@tcp(localhost)/mydb?charset=utf8&parseTime=true"

Reason: After searching, some friends said that this error is due to an incompatible error when storing the value of type uint8 into the time.Time type Scan error. This may be because the data type of the “created_at” column in the database does not match the time.Time type defined in the program. To fix this, you need to check the datatype of the “created_at” column in your database and make sure it matches the type defined in your program. If necessary, you can try to convert the value of type []uint8 to the appropriate time format before storing it in *time.Time type. You can use the Parse method in the time package to convert the []uint8 type value to the time.Time type:

import ( "database/sql" "fmt" "time" ) // ... var createdAt time.Time if err := row.Scan(&createdAt); err != nil { log.Fatal(err) } // Assume that the value of type []uint8 is a string representing time, such as "2022-01-01 00:00: 00" inputTimeStr := "2022-01-01 00:00:00" inputTime, err := time.Parse("2006-01-02 15:04:05", inputTimeStr) if err != nil { log.Fatal (err) } fmt.Println(inputTime) // Output: 2022-01-01 00:00:00 0000 UTC

Of course, the easiest way is to add parameters, but why is it possible after setting parseTime=true? parseTime=true: This parameter tells the MySQL driver to use time parsing when parsing the datetime type in the database to Go’s time.Time type. If this parameter is not set, the MySQL driver will parse the datetime type into a raw []uint8 byte array, resulting in an error. Therefore, when you set parseTime=true when connecting to the database, the MySQL driver will parse the date and time type in the correct way, avoiding the errors encountered.