![Learn Data Structures and Algorithms with Golang](https://wfqqreader-1252317822.image.myqcloud.com/cover/744/36698744/b_36698744.jpg)
上QQ阅读APP看书,第一时间看更新
The delete operation
The delete operation is as follows. The DeleteCustomer method takes the Customer parameter and creates a prepared statement for the DELETE statement. The statement is used to execute the deletion of a customer row in the table:
// Delete Customer method with parameter customer
func deleteCustomer(customer Customer){
var database *sql.DB
database= GetConnection()
var error error
var delete *sql.Stmt
delete,error = database.Prepare("DELETE FROM Customer WHERE Customerid=?")
if error != nil {
panic(error.Error())
}
delete.Exec(customer.CustomerId)
defer database.Close()
}
// main method
func main() {
var customers []Customer
customers = GetCustomers()
fmt.Println("Before Delete",customers)
var customer Customer
customer.CustomerName = "George Thompson"
customer.SSN = "23233432"
customer.CustomerId = 5
deleteCustomer(customer)
customers = GetCustomers()
fmt.Println("After Delete",customers)
}
Run the following commands:
go run database_operations.go
The following screenshot displays the output:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/780cc346-b9e8-4eb6-aee9-e3f122519c17.png?sign=1738870530-tGZ6R2By62E6q17ozpwB4YXTDY1zwi6E-0-72973cd7232f30ed4d4775e38090bf71)
Now that we are done with variadic functions, let's go ahead and look at CRUD web forms in the next section.