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

此時(shí)在在瀏覽器中輸入127.0.0.1:9300/index。會(huì)報(bào)出404,于此同時(shí),控制臺(tái)中會(huì)打印相關(guān)錯(cuò)誤,這個(gè)功能也是我很喜歡的一個(gè)功能。

3. 編寫路由
服務(wù)已經(jīng)啟動(dòng)了,接下來看看如何創(chuàng)建 gin 的路由。和前文一樣,我們先來創(chuàng)建一個(gè)處理 GET 請(qǐng)求的路由/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請(qǐng)求 - 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。
你會(huì)發(fā)現(xiàn)**<h1>這個(gè)標(biāo)簽沒有被瀏覽器識(shí)別,而是以字符串的形式輸出了**。

這是因?yàn)?gin 框架中對(duì)輸出做出了嚴(yán)格的分類,在?c.String()?函數(shù)中輸出的值只會(huì)是字符串的形式輸出,這是在原生函數(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語言實(shí)戰(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="請(qǐng)輸入賬號(hào)"> - 15? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 16? ? ? ? ? ? ? ? ? ? ? ?
</div> - 17? ? ? ? ? ? ? ? ? ? ? ?
<div> - 18? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div> - 19? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<input type="password" class="form-control" id="password" name="password" placeholder="請(qǐng)輸入密碼"> - 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ù)拿出來作為一個(gè)變量,代碼修改后如下所示:
- 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 來寫一個(gè) post 服務(wù) check 用來接收驗(yàn)證登錄頁面發(fā)送過來的賬號(hào)密碼。
代碼示例:
- 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('賬號(hào)或者密碼不正確')</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語言實(shí)戰(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。

輸入正確的賬號(hào):Codey,密碼:12345

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

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

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

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