Deleting Data
The deletion of data can happen 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 it. 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 from the database.
For the sake of simplicity, we 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://packt.live/371GoCy
We update the line with the Exec()
statement:
DeleteResult, DeleteResultErr := db.Exec(DeleteStatement,2) if DeleteResultErr != nil { panic(DeleteResultErr) }
Also, we update the line with the calculation...