stmt, err := db.Prepare("INSERT INTO users (name, email) VALUES (?, ?)") if err != nil { // handle error } res, err := stmt.Exec("John Doe", "[email protected]") if err != nil { // handle error } rowsAffected, err := res.RowsAffected() if err != nil { // handle error } lastInsertID, err := res.LastInsertId() if err != nil { // handle error }
stmt, err := db.Prepare("UPDATE users SET name=? WHERE email=?") if err != nil { // handle error } res, err := stmt.Exec("Jane Doe", "[email protected]") if err != nil { // handle error } rowsAffected, err := res.RowsAffected() if err != nil { // handle error }In this example, we prepare an SQL statement to update the name of a user in the "users" table. We execute the statement with two parameters, "Jane Doe" (the new name) and "[email protected]" (the email of the user to update). We then use the Result interface to get the number of rows affected by the statement (which should be 1, assuming there is a user with that email in the table). Overall, the Result interface provides a convenient way to work with the outcomes of SQL statements in Go. It is part of the "database/sql" package library, which is included in the standard Go distribution.