Deleting data
Data can be deleted for multiple reasons: we don’t need the data anymore, we are migrating to another database, or we are replacing the current solution. We are in luck because the current Go facilities provide a very nice way to do this. The analogy is the same as for the UPDATE
statement of our records. We formulate a DELETE
statement and execute it; we can technically modify the action of our UPDATE
script to delete it from the database.
For the sake of simplicity, we’ll only modify the relevant lines. Our DELETE
statement will replace the UPDATE
statement, like this:
DBDelete.go
12 DeleteStatement :=` 13 DELETE FROM test 14 WHERE id = $1 15 `
The full code is available at https://github.com/PacktPublishing/Go-Programming-From-Beginner-to-Professional-Second-Edition-/blob/main/Chapter15/Examples/DBDelete.go. We’ll update the line with the Exec()
statement:
deleteResult, deleteResultErr...