Updating your first record in MySQL
Consider a scenario where you have created a record for an employee in a database with all its details, such as name, department, address, and so on, and after some time the employee changes departments. In that case, we have to update their department in a database so that their details are in sync all across the organization, which can be achieved using a SQL UPDATE
statement, and in this recipe we will learn how we can implement it in Go.
How to do it…
- Install the
github.com/go-sql-driver/mysql
andgithub.com/gorilla/mux
packages, using thego get
command, as follows:
$ go get github.com/go-sql-driver/mysql $ go get github.com/gorilla/mux
- Create
update-record-mysql.go
. Then we connect to the MySQL database, update the name of an employee for an ID, and write the number of records updated in a database to an HTTP response stream, as follows:
package main import ( "database/sql" "fmt" "log" "net/http" "github.com/go-sql-driver/mysql" "github...