More articles -> ISLAND
What is a Gin
Gin is an HTTP Web framework written in Golang.
This is the description from Gin Github.
The development environment
- GoLand 2019.2 EAP
- GoLang 1.11.5
- Use Go Modules for management
Quick start
When we use GoLand to create a new project, we select Go Modules(vgo) and fill in our project address and project name, which we’ll call GinHello.
Click Create and Goland generates the project directory for us. The Go project directory is always simple, much simpler than the project directory generated by Maven or Gradle in Java.
GinHello
|
|-go.mod
Copy the code
Yes, it’s a file, a Go Module file. Go Mod is an official dependency management tool introduced by Go.
Add the dependent
Rely on the go mod file.
The require github.com/gin-gonic/gin v1.4.0
We add the above dependencies to the Go Module and goLand automatically downloads and manages the dependencies for us.
Hello Gin
When the dependencies are added, you can start writing code.
Create a new main.go file.
package main
import (
"github.com/gin-gonic/gin"
)
func main(a) {
router := gin.Default()
router.Run()
}
Copy the code
Gin only needs two lines of code to get our service running.
As soon as we hit Run, the project will launch a port 8080, open the browser localhost:8080 and we’ll see a 404 Page not found, because our root route didn’t return any results. We can also see some printed information on the console, including the port where we just accessed the root route.
Produce interface
The project has started, so how do I return an interface?
Configure the parameters we return using the Handle of the router.
// omit the code
// Add a Get request route
r.GET("/".func(context *gin.Context) {
context.String(http.StatusOK, "hello gin")})// omit the code
Copy the code
At this point we restart the project and revisit the page localhost:808, which now shows Hello Gin.
We can also POST,PUT, and DELETE requests.
Unit testing
Unit test is an indispensable module of a project, and also an important dependence to ensure the normal operation of the project. Let’s unit test Gin.
To facilitate unit testing, we first need to do an extraction of our project.
Create a new folder called initRouter
Create the go file initrouter. go
package initRouter
import (
"github.com/gin-gonic/gin"
"net/http"
)
func SetupRouter(a) *gin.Engine {
router := gin.Default()
// Add a Get request route
router.GET("/".func(context *gin.Context) {
context.String(http.StatusOK, "hello gin")})return router
}
Copy the code
Also modify main.go
package main
import (
"GinHello/initRouter"
)
func main(a) {
router := initRouter.SetupRouter()
_ = router.Run()
}
Copy the code
Completed the preliminary establishment of the project test.
Set up the test directory. Golang’s unit tests end with _test. Set up the index_test.go file.
package test
import (
"GinHello/initRouter"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestIndexGetRouter(t *testing.T) {
router := initRouter.SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/".nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "hello gin", w.Body.String())
}
Copy the code
Assert to determine whether the returned status code and value are consistent with the values in the code.
The project directory is:
GinHello
|
|-initRouter
| |-initRouter.go
|
|-test
| |-index_test.go
|
|-main.go
|-go.mod
|-go.sum
Copy the code
Run the unit tests, and the console prints out the unit test results.
— PASS: TestIndexGetRouter (0.02s) PASS
conclusion
By simply setting up a Gin project, you can see how easy and convenient it is to set up an Http server with Go, and get the project up and running with zero configuration.
Code for this section
Github