There is such a function:

func EditAddNar(rnd render.Render, r *http.Request) { r.ParseForm() id_patient := r.FormValue("id_patient") id_vrach_ortoped := r.FormValue("id_vrach_ortoped") id_vrach_technic := r.FormValue("id_vrach_technic") number_nar := r.FormValue("number_nar") date_open_nar := r.FormValue("date_open_nar") date_start_production := r.FormValue("date_start_production") date_close_nar := r.FormValue("date_close_nar") sum := r.FormValue("sum") switch id_vrach_ortoped { case "": id_vrach_ortoped = "null" } switch id_vrach_technic { case "": id_vrach_technic = "null" } switch number_nar { case "": number_nar = "null" } switch date_open_nar { case "": date_open_nar = "null" } switch date_start_production { case "": date_start_production = "null" } switch date_close_nar { case "": date_close_nar = "null" } switch &sum { case "": sum = "null" } var query = "INSERT INTO j_nar (id_patient, id_vrach_ortoped, id_vrach_technic, number_nar, date_open_nar ,date_start_production, da te_close_nar, sum) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)" models.ModelAddNar(query, id_patient, id_vrach_ortoped, id_vrach_technic, number_nar, date_open_nar, date_start_production, date_close_nar, sum) } 

models.ModelAddNar simply passes the request and parameters to the model, and she already executes the command. pq swears:

pq: invalid input syntax for integer: "NULL"

But if all the data from r.FormParse () were not empty, then the query is working. Tell me how to insert empty (null) values ​​into the database.

    1 answer 1

    I do it like this:

     func NullableFloat (s string) sql.NullFloat64 { if s != "" { value, err := strconv.ParseFloat(s, 64) if err == nil { return sql.NullFloat64{value, true} } } return sql.NullFloat64{} } func NullableInt (s string) sql.NullInt64 { if s != "" { value, err := strconv.ParseInt(s, 10, 64) if err == nil { return sql.NullInt64{value, true} } } return sql.NullInt64{} } func NullableString (s string) sql.NullString { return sql.NullString{s, s != ""} } func EditAddNar(rnd render.Render, r *http.Request) { r.ParseForm() id_patient := r.FormValue("id_patient") id_vrach_ortoped := r.FormValue("id_vrach_ortoped") id_vrach_technic := r.FormValue("id_vrach_technic") number_nar := r.FormValue("number_nar") date_open_nar := r.FormValue("date_open_nar") date_start_production := r.FormValue("date_start_production") date_close_nar := r.FormValue("date_close_nar") sum := r.FormValue("sum") models.ModelAddNar(`INSERT INTO j_nar ( id_patient, id_vrach_ortoped, id_vrach_technic, number_nar, date_open_nar, date_start_production, date_close_nar, sum ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`, id_patient, NullableInt(id_vrach_ortoped), NullableInt(id_vrach_technic), NullableInt(number_nar), NullableString(date_open_nar), NullableString(date_start_production), NullableString(date_close_nar), NullableFloat(sum)) } 

    See Go: Working effectively with database nulls for more information on NULL abeln values. Also database / sql documentation will be useful.

    PS If you are interested, you can see more convenient work with the generation of SQL expressions "on the fly" https://play.golang.org/p/vR-BEwGBef