使用 Go 語言搭建簡易登錄功能
在這篇文章之前,已經學完了 Go 語言所有基礎特性,對 Go 語言也有了一定掌握和理解。本文就來學習如何使用 Go 語言如何搭建一個 web 服務。這個 web 服務主要提供登錄的功能。
1. 搭建服務
在 Go 語言中想要搭建一個 http 服務是非常容易的一件事情,一行代碼就可以了。
代碼示例:
- 1?
package main - 2
- 3?
import ( - 4? ? ? ?
"net/http" - 5?
) - 6
- 7?
func main() { - 8? ? ? ?
http.ListenAndServe("127.0.0.1:9300", nil) //設置監聽的端口 - 9?
}
運行以上代碼可以得到一個服務,在瀏覽器上輸入http://127.0.0.1:9300/,由于沒有編寫任何路由,所以只會出現 404 的提示:

2. 編寫路由
服務已經可以運行了,接下來就是要編寫能被外部訪問的路由接口,http 請求分為兩種,POST 請求和 GET 請求。我們首先想實現的是一個網站登錄頁面打開的路由?/index,需要編寫一個能響應 GET 請求的路由。
代碼示例:
- 1?
package main - 2
- 3?
import ( - 4? ? ? ? ?
"net/http" - 5?
) - 6
- 7?
func main() { - 8? ? ? ? ?
//設置訪問的路由 - 9? ? ? ? ?
http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) { - 10? ? ? ? ? ? ? ?
if r.Method == "GET" { - 11? ? ? ? ? ? ? ? ? ? ? ?
w.Write([]byte("<h1>Hello Codey!<h1>")) - 12? ? ? ? ? ? ? ?
} - 13? ? ? ?
}) - 14? ? ? ?
http.ListenAndServe("127.0.0.1:9300", nil) //設置監聽的端口 - 15?
}
在瀏覽器中輸入127.0.0.1:9300/index:

此處可以結合函數式編程的思想,將 index 的處理函數拿出來作為一個變量,代碼修改后如下所示
- 1?
package main - 2
- 3?
import ( - 4? ? ? ? ?
"net/http" - 5?
) - 6
- 7?
func main() { - 8? ? ? ? ?
http.HandleFunc("/index", index) //設置訪問的路由 - 9? ? ? ? ?
http.ListenAndServe("127.0.0.1:9300", nil) //設置監聽的端口 - 10?
} - 11
- 12?
func index(w http.ResponseWriter, r *http.Request) { - 13? ? ? ??
if r.Method == "GET" { - 14? ? ? ? ? ? ? ?
w.Write([]byte("<h1>Hello Codey!<h1>")) - 15? ? ? ?
} - 16?
}
然后修改一下輸出字符串,使其輸出一個頁面,代碼修改后如下
- 1?
package main - 2
- 3?
import ( - 4? ? ? ?
"net/http" - 5?
) - 6
- 7?
func main() { - 8? ? ? ? ?
http.HandleFunc("/index", index) //設置訪問的路由 - 9? ? ? ? ?
http.ListenAndServe("127.0.0.1:9300", nil) //設置監聽的端口 - 10?
} - 11
- 12?
func index(w http.ResponseWriter, r *http.Request) { - 13? ? ? ?
if r.Method == "GET" { - 14? ? ? ? ? ? ? ?
w.Write([]byte(`<!DOCTYPE html> - 15? ? ? ? ? ? ? ?
<html> - 16? ? ? ? ? ? ? ??
<head> - 17? ? ? ? ? ? ? ? ? ? ? ?
<meta charset="utf-8"> - 18? ? ? ? ? ? ? ? ? ? ? ?
<title>Go語言實戰1</title> - 19? ? ? ? ? ? ? ?
</head> - 20? ? ? ? ? ? ? ?
<body> - 21? ? ? ? ? ? ? ? ? ? ? ?
<div> - 22? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<h3>登錄</h3> - 23? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<form> - 24? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div> - 25? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div> - 26? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<input type="text" id="username" name="username" placeholder="請輸入賬號"> - 27? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 28? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 29? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div> - 30? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div> - 31? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<input type="password" class="form-control" id="password" name="password" placeholder="請輸入密碼"> - 32? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 33? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 34? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div > - 35? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div > - 36? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<button id="loginbtn" type="button" >登錄</button> - 37? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 38? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 39? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</form> - 40? ? ? ? ? ? ? ? ? ? ?
</div> - 41? ? ? ? ? ? ?
</body> - 42? ? ? ? ? ? ?
</html>`)) - 43? ? ? ?
} - 44?
}
運行上述代碼,然后再次在瀏覽器中輸入127.0.0.1:9300/index。

3. 配置頁面到 html
一般寫 web 應用,會涉及到很多 html 文件,我們不可能將其全部都放在 Go 文件的字符串里,不方便調試的同時也影響代碼維護。所以我們一般會直接加載 html 文件。
代碼示例:
- 1?
package main - 2
- 3?
import ( - 4? ? ? ? ?
"net/http" - 5? ? ? ? ?
"text/template" - 6?
) - 7
- 8?
func main() { - 9? ? ? ? ?
http.HandleFunc("/index", index) //設置訪問的路由 - 10? ? ? ?
http.ListenAndServe("127.0.0.1:9300", nil) //設置監聽的端口 - 11 ?
} - 12
- 13?
func index(w http.ResponseWriter, r *http.Request) { - 14? ? ? ?
if r.Method == "GET" { - 15? ? ? ? ? ? ? ?
t, _ := template.ParseFiles("view/index.html")//加載html文件 - 16? ? ? ? ? ? ? ?
t.Execute(w, nil)//將文件輸出到瀏覽器 - 17? ? ? ??
} - 18?
}
目錄結構如下

index.html 的代碼如下:
- 1?
<!DOCTYPE html> - 2?
<html> - 3
- 4?
<head> - 5? ? ? ? ?
<meta charset="utf-8"> - 6? ? ? ? ?
<title>Go語言實戰1</title> - 7?
</head> - 8?
<body> - 9? ? ? ? ?
<div> - 10? ? ? ? ? ? ? ?
<h3>登錄</h3> - 11? ? ? ? ? ? ? ?
<form> - 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" id="password" name="password" placeholder="請輸入密碼"> - 20? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 21? ? ? ? ? ? ? ? ? ? ? ?
</div> - 22? ? ? ? ? ? ? ? ? ? ? ?
<div > - 23? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<div > - 24? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
<button id="loginbtn" type="button" >登錄</button> - 25? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
</div> - 26? ? ? ? ? ? ? ? ? ? ? ?
</div> - 27? ? ? ? ? ? ? ? ?
</form> - 28? ? ? ? ?
</div> - 29 ?
</body> - 30?
</html>
執行上述 Go 語言代碼,在瀏覽器中輸入127.0.0.1:9300/index。

4. 數據傳輸
在 html 頁面點擊登錄暫時沒有任何反應,為了提交頁面到服務端,我們需要在服務端再編寫一個接收數據的路由,這個路由需要能夠接收 POST 請求。然后再這個路由中需要能驗證賬號密碼是否正確,若是則跳轉到主頁,若不是則給出提示后跳轉到登錄頁。
代碼示例
- 1?
package main - 2
- 3?
import ( - 4? ? ? ? ?
"net/http" - 5? ? ? ? ?
"text/template" - 6?
) - 7
- 8?
func main() { - 9? ? ? ? ?
http.HandleFunc("/index", index) //設置訪問的路由 - 10
- 11? ? ? ?
http.HandleFunc("/check", check) - 12? ? ? ?
http.ListenAndServe("127.0.0.1:9300", nil) //設置監聽的端口 - 13 ?
} - 14
- 15?
func check(w http.ResponseWriter, r *http.Request) { - 16? ? ? ?
if r.Method == "POST" { - 17? ? ? ? ? ? ? ?
accountID := r.FormValue("username")//獲取賬號 - 18? ? ? ? ? ? ? ?
password := r.FormValue("password")//獲取密碼 - 19? ? ? ? ? ? ? ?
if accountID == "Codey" && password == "12345" { - 20? ? ? ? ? ? ? ? ? ? ? ?
//跳轉到主頁 - 21? ? ? ? ? ? ? ? ? ? ? ?
t, _ := template.ParseFiles("view/home.html") - 22? ? ? ? ? ? ? ? ? ? ? ?
t.Execute(w, nil) - 23? ? ? ? ? ? ? ?
} else { - 24? ? ? ? ? ? ? ? ? ? ? ?
//跳轉到登錄 - 25? ? ? ? ? ? ? ? ? ? ? ?
w.Write([]byte("<script>alert('賬號或者密碼不正確')</script>")) - 26? ? ? ? ? ? ? ? ? ? ? ?
t, _ := template.ParseFiles("view/index.html") - 27? ? ? ? ? ? ? ? ? ? ? ?
t.Execute(w, nil) - 28? ? ? ? ? ? ? ? ?
} - 29
- 30? ? ? ?
} - 31?
} - 32
- 33 ?
func index(w http.ResponseWriter, r *http.Request) { - 34? ? ? ?
if r.Method == "GET" { - 35? ? ? ? ? ? ?
t, _ := template.ParseFiles("view/index.html") - 36? ? ? ? ? ? ?
t.Execute(w, nil) - 37? ? ? ?
} - 38?
}
home.html 的代碼如下:
- 1?
<!DOCTYPE html> - 2?
<html> - 3
- 4?
<head> - 5? ? ? ? ?
<meta charset="utf-8"> - 6? ? ? ? ?
<title>Go語言實戰1</title> - 7?
</head> - 8?
<body> - 9? ? ? ? ?
<div> - 10? ? ? ? ? ? ? ?
<h3>主頁</h3> - 11? ? ? ? ? ? ? ?
這里是主頁 - 12? ? ? ?
</div> - 13?
</body> - 14?
</html>
執行上述 Go 語言代碼,在瀏覽器中輸入127.0.0.1:9300/index。

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

然后點擊登錄,會跳轉到主頁

若輸入錯誤的賬號密碼,則不跳轉

隨后跳轉回登錄頁面

一個簡易的登錄功能就搭建完成了。
5. 小結
本文主要介紹了 Go 語言官方提供的 http 服務,以及如何使用這個包來搭建一個 web 應用。其中需要注意區分前端發送過來的請求類型,POST 和 GET 兩個請求各自有各自的處理。
文章來源于網絡,侵刪!
