使用 Go 語言搭建簡易登錄功能
在這篇文章之前,已經(jīng)學(xué)完了 Go 語言所有基礎(chǔ)特性,對 Go 語言也有了一定掌握和理解。本文就來學(xué)習(xí)如何使用 Go 語言如何搭建一個(gè) web 服務(wù)。這個(gè) web 服務(wù)主要提供登錄的功能。
1. 搭建服務(wù)
在 Go 語言中想要搭建一個(gè) http 服務(wù)是非常容易的一件事情,一行代碼就可以了。
代碼示例:
package main
import (
"net/http"
)
func main() {
http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}
運(yùn)行以上代碼可以得到一個(gè)服務(wù),在瀏覽器上輸入http://127.0.0.1:9300/
,由于沒有編寫任何路由,所以只會(huì)出現(xiàn) 404 的提示:
2. 編寫路由
服務(wù)已經(jīng)可以運(yùn)行了,接下來就是要編寫能被外部訪問的路由接口,http 請求分為兩種,POST 請求和 GET 請求。我們首先想實(shí)現(xiàn)的是一個(gè)網(wǎng)站登錄頁面打開的路由 /index
,需要編寫一個(gè)能響應(yīng) GET 請求的路由。
代碼示例:
package main
import (
"net/http"
)
func main() {
//設(shè)置訪問的路由
http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.Write([]byte("<h1>Hello Codey!<h1>"))
}
})
http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}
在瀏覽器中輸入127.0.0.1:9300/index
:
此處可以結(jié)合函數(shù)式編程的思想,將 index 的處理函數(shù)拿出來作為一個(gè)變量,代碼修改后如下所示
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/index", index) //設(shè)置訪問的路由
http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.Write([]byte("<h1>Hello Codey!<h1>"))
}
}
然后修改一下輸出字符串,使其輸出一個(gè)頁面,代碼修改后如下
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/index", index) //設(shè)置訪問的路由
http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.Write([]byte(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go語言實(shí)戰(zhàn)1</title>
</head>
<body>
<div>
<h3>登錄</h3>
<form>
<div>
<div>
<input type="text" id="username" name="username" placeholder="請輸入賬號(hào)">
</div>
</div>
<div>
<div>
<input type="password" class="form-control" id="password" name="password" placeholder="請輸入密碼">
</div>
</div>
<div >
<div >
<button id="loginbtn" type="button" >登錄</button>
</div>
</div>
</form>
</div>
</body>
</html>`))
}
}
運(yùn)行上述代碼,然后再次在瀏覽器中輸入127.0.0.1:9300/index
。
3. 配置頁面到 html
一般寫 web 應(yīng)用,會(huì)涉及到很多 html 文件,我們不可能將其全部都放在 Go 文件的字符串里,不方便調(diào)試的同時(shí)也影響代碼維護(hù)。所以我們一般會(huì)直接加載 html 文件。
代碼示例:
package main
import (
"net/http"
"text/template"
)
func main() {
http.HandleFunc("/index", index) //設(shè)置訪問的路由
http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("view/index.html")//加載html文件
t.Execute(w, nil)//將文件輸出到瀏覽器
}
}
目錄結(jié)構(gòu)如下
index.html 的代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go語言實(shí)戰(zhàn)1</title>
</head>
<body>
<div>
<h3>登錄</h3>
<form>
<div>
<div>
<input type="text" id="username" name="username" placeholder="請輸入賬號(hào)">
</div>
</div>
<div>
<div>
<input type="password" id="password" name="password" placeholder="請輸入密碼">
</div>
</div>
<div >
<div >
<button id="loginbtn" type="button" >登錄</button>
</div>
</div>
</form>
</div>
</body>
</html>
執(zhí)行上述 Go 語言代碼,在瀏覽器中輸入127.0.0.1:9300/index
。
4. 數(shù)據(jù)傳輸
在 html 頁面點(diǎn)擊登錄暫時(shí)沒有任何反應(yīng),為了提交頁面到服務(wù)端,我們需要在服務(wù)端再編寫一個(gè)接收數(shù)據(jù)的路由,這個(gè)路由需要能夠接收 POST 請求。然后再這個(gè)路由中需要能驗(yàn)證賬號(hào)密碼是否正確,若是則跳轉(zhuǎn)到主頁,若不是則給出提示后跳轉(zhuǎn)到登錄頁。
代碼示例
package main
import (
"net/http"
"text/template"
)
func main() {
http.HandleFunc("/index", index) //設(shè)置訪問的路由
http.HandleFunc("/check", check)
http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}
func check(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
accountID := r.FormValue("username")//獲取賬號(hào)
password := r.FormValue("password")//獲取密碼
if accountID == "Codey" && password == "12345" {
//跳轉(zhuǎn)到主頁
t, _ := template.ParseFiles("view/home.html")
t.Execute(w, nil)
} else {
//跳轉(zhuǎn)到登錄
w.Write([]byte("<script>alert('賬號(hào)或者密碼不正確')</script>"))
t, _ := template.ParseFiles("view/index.html")
t.Execute(w, nil)
}
}
}
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("view/index.html")
t.Execute(w, nil)
}
}
home.html 的代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go語言實(shí)戰(zhàn)1</title>
</head>
<body>
<div>
<h3>主頁</h3>
這里是主頁
</div>
</body>
</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)回登錄頁面
一個(gè)簡易的登錄功能就搭建完成了。
5. 小結(jié)
本文主要介紹了 Go 語言官方提供的 http 服務(wù),以及如何使用這個(gè)包來搭建一個(gè) web 應(yīng)用。其中需要注意區(qū)分前端發(fā)送過來的請求類型,POST 和 GET 兩個(gè)請求各自有各自的處理。