Using pointers for sql-scan destination variables allows you to check the data, process it (provided that if! = Nil is checked) and sort by json, which should be sent from the API, without the need to put hundreds of sql.Nullstring, sql.Nullfloat64, etc. . everywhere. Stockings are wonderfully preserved and sent through Marshall John. (See Name of Name below). At the other end, the client can work with zeros in javascript that are better equipped to handle them.
func queryToJson(db *sql.DB) []byte { rows, err := db.Query( "select mothername, fathername, surname from fams" + "where surname = ?", "Nullfather" ) defer rows.Close() type record struct { Mname, Fname, Surname *string // the key: use pointers } records := []record{} for rows.Next() { var r record err := rows.Scan(r.Mname, r.Fname, r.Surname) // no need for "&" if err != nil { log.Fatal(err) } fmt.Println(r) records = append(records, r) } j, err := json.Marshal(records) if err != nil { log.Fatal(err) } return j } j := queryToJson(db) fmt.Println(string(j)) // [{"Mothername":"Mary", "Fathername":null, "Surname":"Nullfather"}]
Josh b
source share