O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works…

Once we run the program, the HTTP server will start locally listening on port 8080

Executing a POST request to create an employee record from the command line as follows will give you the ID of the last record created:

$ curl -X POST http://localhost:8080/employee/create?name=fooLast created record id is :: 1

Let’s understand the program we have written:

  1. Using import ("database/sql" "fmt" "log" "net/http" "strconv" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux"), we imported github.com/gorilla/mux to create a Gorilla Mux Router and initialized the Go MySQL driver, importing the github.com/go-sql-driver/mysql package.
  2. Next, we defined a createRecord handler, which fetches the name from the request, assigns it ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required