使用 gin 包優(yōu)化登錄功能
上一個實戰(zhàn)文章我們學(xué)習(xí)了如何使用 Go 語言原生的 http 包來構(gòu)建一個 web 應(yīng)用,實現(xiàn)了一個簡單的登錄功能。因為原生的 http 包很多功能都需要自己去寫,所以就有很多開發(fā)者在原生包的基礎(chǔ)上開發(fā)了第三方包。本文就來介紹一個開發(fā) Go web 十分流行的包——?gin 包。其官方地址為:https://github.com/gin-gonic/gin。
1. 下載 gin 包
因為是第三方包,所以需要從 github 上下載后才可使用。使用以下指令下載使用:
- 1?
go get https://github.com/gin-gonic/gin
可能會比較久,如果覺得下載不便的話,可以直接使用 go mod 來構(gòu)建項目,就可以跳過下載這個步驟。
2. 搭建服務(wù)
本文將使用 go mod 來構(gòu)建項目,所以先在一個空白項目中執(zhí)行
- 1?
go mod init
然后再在 Go 文件寫入以下代碼構(gòu)建 web 服務(wù),代碼示例:
- 1?
package main
- 2
- 3?
import (
- 4? ? ? ? ?
"github.com/gin-gonic/gin"//導(dǎo)入gin包
- 5?
)
- 6
- 7?
func main() {
- 8? ? ? ? ?
router := gin.Default()//實例化一個gin
- 9? ? ? ? ?
router.Run("127.0.0.1:9300")//監(jiān)聽9300端口
- 10?
}
啟動述代碼就可以啟動一個 gin 的 web 服務(wù),此時會打印一些 gin 的日志,表示服務(wù)已啟動。
Tips:如果你事前沒有g(shù)in包,gomod會幫你自動下載。

此時在在瀏覽器中輸入127.0.0.1:9300/index
。會報出404,于此同時,控制臺中會打印相關(guān)錯誤,這個功能也是我很喜歡的一個功能。

3. 編寫路由
服務(wù)已經(jīng)啟動了,接下來看看如何創(chuàng)建 gin 的路由。和前文一樣,我們先來創(chuàng)建一個處理 GET 請求的路由/index
。
代碼示例:
- 1?
package main
- 2
- 3?
import (
- 4? ? ? ? ?
"net/http"
- 5
- 6? ? ? ? ?
"github.com/gin-gonic/gin"
- 7?
)
- 8
- 9?
func main() {
- 10? ? ? ?
router := gin.Default()
- 11? ? ? ?
//創(chuàng)建get請求
- 12? ? ? ?
router.GET("/index", func(c *gin.Context) {
- 13? ? ? ? ? ? ? ? ?
c.String(http.StatusOK, "<h1>Hello Codey!</h1>")
- 14? ? ? ?
})
- 15? ? ? ?
router.Run("127.0.0.1:9300")
- 16?
}
執(zhí)行上述代碼之后,在瀏覽器中輸入127.0.0.1:9300/index
。
你會發(fā)現(xiàn)**<h1>
這個標(biāo)簽沒有被瀏覽器識別,而是以字符串的形式輸出了**。

這是因為 gin 框架中對輸出做出了嚴(yán)格的分類,在?c.String()
?函數(shù)中輸出的值只會是字符串的形式輸出,這是在原生函數(shù)之上為了**防止XSS(****跨站腳本攻擊)**而做的優(yōu)化。所以它無法直接打印 html 腳本來渲染頁面,必須要使用?c.HTML()
?函數(shù)來加載 html 文件。
代碼示例:
- 1?
package main
- 2
- 3?
import (
- 4? ? ? ? ?
"net/http"
- 5
- 6? ? ? ? ?
"github.com/gin-gonic/gin"
- 7?
)
- 8
- 9?
func main() {
- 10? ? ? ?
router := gin.Default()
- 11? ? ? ?
router.LoadHTMLGlob("view/*")//設(shè)定html存放的文件目錄
- 12? ? ? ?
router.GET("/index", func(c *gin.Context) {
- 13? ? ? ? ? ? ? ? ?
c.HTML(http.StatusOK, "index.html", nil)//打開view.index.html
- 14? ? ? ?
})
- 15? ? ? ?
router.Run("127.0.0.1:9300")
- 16?
}
目錄結(jié)構(gòu)如下

index.html 代碼如下:
- 1?
<!DOCTYPE html>
- 2?
<html>
- 3
- 4?
<head>
- 5? ?? ?? ?
<meta charset="utf-8">
- 6? ? ? ? ?
<title>Go語言實戰(zhàn)2</title>
- 7?
</head>
- 8?
<body>
- 9? ? ? ? ?
<div>
- 10? ? ? ? ? ? ? ?
<h3>登錄</h3>
- 11? ? ? ? ? ? ? ?
<form action="check" method="POST">
- 12? ? ? ? ? ? ? ? ? ? ? ?
<div>
- 13? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div>
- 14? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<input type="text" id="username" name="username" placeholder="請輸入賬號">
- 15? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div>
- 16? ? ? ? ? ? ? ? ? ? ? ?
</div>
- 17? ? ? ? ? ? ? ? ? ? ? ?
<div>
- 18? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div>
- 19? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<input type="password" class="form-control" id="password" name="password" placeholder="請輸入密碼">
- 20? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div>
- 21? ? ? ? ? ? ? ? ? ? ? ?
</div>
- 22? ? ? ? ? ? ? ? ? ? ? ?
<div >
- 23? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div >
- 24? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<button id="loginbtn" type="submit" >登錄</button>
- 25? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div>
- 26? ? ? ? ? ? ? ? ? ? ? ?
</div>
- 27? ? ? ? ? ? ? ?
</form>
- 28? ? ? ?
</div>
- 29?
</body>
- 30?
</html>
執(zhí)行上述代碼之后,在瀏覽器中輸入127.0.0.1:9300/index
。

此處可以結(jié)合函數(shù)式編程的思想,將 index 的處理函數(shù)拿出來作為一個變量,代碼修改后如下所示:
- 1?
package main
- 2
- 3?
import (
- 4? ? ? ? ?
"net/http"
- 5
- 6? ? ? ? ?
"github.com/gin-gonic/gin"
- 7?
)
- 8
- 9?
func main() {
- 10? ? ? ?
router := gin.Default()
- 11? ? ? ?
router.LoadHTMLGlob("view/*")
- 12? ? ? ?
router.GET("/index", index)
- 13? ? ? ?
router.Run("127.0.0.1:9300")
- 14?
}
- 15
- 16?
func index(c *gin.Context) {
- 17? ? ? ?
c.HTML(http.StatusOK, "index.html", nil)
- 18?
}
4. 數(shù)據(jù)傳輸
然后我們就用 gin 來寫一個 post 服務(wù) check 用來接收驗證登錄頁面發(fā)送過來的賬號密碼。
代碼示例:
- 1?
package main
- 2
- 3?
import (
- 4? ? ? ? ?
"fmt"
- 5? ? ? ? ?
"net/http"
- 6
- 7? ? ? ? ?
"github.com/gin-gonic/gin"
- 8?
)
- 9
- 10?
func main() {
- 11? ? ? ? ?
router := gin.Default()
- 12? ? ? ? ?
router.LoadHTMLGlob("view/*")
- 13? ? ? ? ?
router.GET("/index", index)
- 14? ? ? ? ?
router.POST("/check", check)
- 15? ? ? ? ?
router.Run("127.0.0.1:9300")
- 16?
}
- 17
- 18?
func index(c *gin.Context) {
- 19? ? ? ? ?
c.HTML(http.StatusOK, "index.html", nil)
- 20?
}
- 21
- 22?
func check(c *gin.Context) {
- 23? ? ? ? ?
accountID, _ := c.GetPostForm("username")
- 24? ? ? ? ?
password, _ := c.GetPostForm("password")
- 25? ? ? ? ?
fmt.Println(accountID, password)
- 26? ? ? ? ?
if accountID == "Codey" && password == "12345" {
- 27? ? ? ? ? ? ? ? ?
//跳轉(zhuǎn)到主頁
- 28? ? ? ? ? ? ? ? ?
c.HTML(http.StatusOK, "home.html", nil)
- 29? ? ? ? ?
} else {
- 30? ? ? ? ? ? ? ? ?
//跳轉(zhuǎn)到登錄
- 31? ? ? ? ? ? ? ? ?
c.Writer.Write([]byte("<script>alert('賬號或者密碼不正確')</script>"))
- 32? ? ? ? ? ? ? ? ?
c.HTML(http.StatusOK, "index.html", nil)
- 33? ? ? ? ?
}
- 34
- 35?
}
home.html 代碼如下:
- 1?
<!DOCTYPE html>
- 2?
<html>
- 3
- 4?
<head>
- 5? ? ? ? ?
<meta charset="utf-8">
- 6? ? ? ? ?
<title>Go語言實戰(zhàn)2</title>
- 7?
</head>
- 8?
<body>
- 9? ? ? ? ?
<div>
- 10? ? ? ? ? ? ? ?
<h3>主頁</h3>
- 11? ? ? ? ? ? ? ?
這里是主頁
- 12? ? ? ?
</div>
- 13?
</body>
- 14?
</html>
執(zhí)行上述 Go 語言代碼,在瀏覽器中輸入127.0.0.1:9300/index
。

輸入正確的賬號:Codey,密碼:12345

然后點擊登錄,會跳轉(zhuǎn)到主頁

若輸入錯誤的賬號密碼,則不跳轉(zhuǎn)

隨后跳轉(zhuǎn)回登錄頁面

在 gin 中一個簡易的登錄功能就搭建完成了。
5. 小結(jié)
本文主要使用了gin這個第三方開發(fā)包,來搭建了一個Go語言的web應(yīng)用,這個gin包的本質(zhì)還是http包,但是它在其上封裝了一層接口,使我們可以更高效的開發(fā)以及開發(fā)出來的應(yīng)用更安全。在我們Go語言的學(xué)習(xí)和開發(fā)的過程中,在熟悉了官方庫之后,我們也可以去使用一些能更好的幫助我們開發(fā)的第三方包。
文章來源于網(wǎng)絡(luò),侵刪!