Answer the question
In order to leave comments, you need to log in
How to process the error code to the database?
Hi everyone
, I have this code:
insert := fmt.Sprintf("insert into la (bla, data_time, kla) values (%d, now(), \"df\"')", ga)
_, err := db.Exec(insert)
if err != nil {
fmt.Println(err)
}
Answer the question
In order to leave comments, you need to log in
db.Exec returns the error interface, you need to try to cast it to *pq.Error in order to process it.
_, err := db.Exec(insert)
if err != nil {
if pgError,ok := err.(*pq.Error); ok {
// Достаём код из переменной pgError
} else {
// Пришедший тип не является *pg.Error
}
}
db.Query("select id, name from users where id = ?", 1) // Вот так, например
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question