Learn Data Structures and Algorithms with Golang
上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:

Now that we are done with variadic functions, let's go ahead and look at CRUD web forms in the next section.