Creating your first record in MySQL
Creating or saving a record in a database requires us to write SQL queries and execute them, implement object-relational mapping (ORM), or implement data-mapping techniques.
In this recipe, we will be writing a SQL query and executing it using the database/sql
package to create a record. To achieve this, you can also implement ORM using any library from a number of third-party libraries available in Go, such as https://github.com/jinzhu/gorm
, https://github.com/go-gorp/gorp
, and https://github.com/jirfag/go-queryset
.
Getting ready…
As we have already established a connection with the MySQL database in our previous recipe, we will just extend it to create a record executing a SQL query.
Before creating a record, we have to create a table in the MySQL database, which we will do by executing the commands shown in the following screenshot:

How to do it…
- Install the
github.com/go-sql-driver/mysql
andgithub.com/gorilla/mux
packages, using thego get
command, as follows...