How to do it…

  1. Install the github.com/go-sql-driver/mysql and github.com/gorilla/mux packages, using the go get command, as follows:
$ go get github.com/go-sql-driver/mysql
$ go get github.com/gorilla/mux
  1. Create create-record-mysql.go. Then we connect to the MySQL database and perform an INSERT query to create an employee record, as follows:
package main
import
(
"database/sql"
"fmt"
"log"
"net/http"
"strconv"
"github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
const
(
CONN_HOST = "localhost"
CONN_PORT = "8080"
DRIVER_NAME = "mysql"
DATA_SOURCE_NAME = "root:password@/mydb"
)
var db *sql.DB
var connectionError error
func init()
{
db, connectionError = sql.Open(DRIVER_NAME, DATA_SOURCE_NAME)
if connectionError != nil
{
log.Fatal("error connecting to database : ", connectionError)
}
}
func createRecord(w http.ResponseWriter, r *http.Request)
{
vals := r.URL.Query()
name, ok := vals["name"]
if ok
{
log.Print("going to insert record in database for name : ",
name[0])
stmt, err := db.Prepare("INSERT employee SET name=?")
if err != nil
{
log.Print("error preparing query :: ", err)
return
}
result, err := stmt.Exec(name[0])
if err != nil
{
log.Print("error executing query :: ", err)
return
}
id, err := result.LastInsertId()
fmt.Fprintf(w, "Last Inserted Record Id is :: %s",
strconv.FormatInt(id, 10))
}
else
{
fmt.Fprintf(w, "Error occurred while creating record in
database for name :: %s", name[0])
}
}
func main()
{
router := mux.NewRouter()
router.HandleFunc("/employee/create", createRecord).
Methods("POST")
defer db.Close()
err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, router)
if err != nil
{
log.Fatal("error starting http server : ", err)
return
}
}
  1. Run the program with the following command: 
$ go run create-record-mysql.go
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.170