亚洲熟女综合色一区二区三区,亚洲精品中文字幕无码蜜桃,亚洲va欧美va日韩va成人网,亚洲av无码国产一区二区三区,亚洲精品无码久久久久久久

使用 Go 語言搭建簡易登錄功能

在這篇文章之前,已經(jīng)學(xué)完了 Go 語言所有基礎(chǔ)特性,對(duì) Go 語言也有了一定掌握和理解。本文就來學(xué)習(xí)如何使用 Go 語言如何搭建一個(gè) web 服務(wù)。這個(gè) web 服務(wù)主要提供登錄的功能。

 

1. 搭建服務(wù)

在 Go 語言中想要搭建一個(gè) http 服務(wù)是非常容易的一件事情,一行代碼就可以了。

代碼示例:

代碼塊
  • 1? package main
  • 2
  • 3? import (
  • 4? ? ? ? "net/http"
  • 5? )
  • 6
  • 7? func main() {
  • 8? ? ? ? http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
  • 9? }

運(yùn)行以上代碼可以得到一個(gè)服務(wù),在瀏覽器上輸入http://127.0.0.1:9300/,由于沒有編寫任何路由,所以只會(huì)出現(xiàn) 404 的提示:

圖片描述

 

2. 編寫路由

服務(wù)已經(jīng)可以運(yùn)行了,接下來就是要編寫能被外部訪問的路由接口,http 請(qǐng)求分為兩種,POST 請(qǐng)求和 GET 請(qǐng)求。我們首先想實(shí)現(xiàn)的是一個(gè)網(wǎng)站登錄頁面打開的路由?/index,需要編寫一個(gè)能響應(yīng) GET 請(qǐng)求的路由。

代碼示例:

代碼塊
  • 1? package main
  • 2
  • 3? import (
  • 4? ? ? ? ? "net/http"
  • 5? )
  • 6
  • 7? func main() {
  • 8? ? ? ? ? //設(shè)置訪問的路由
  • 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) //設(shè)置監(jiān)聽的端口
  • 15? }

在瀏覽器中輸入127.0.0.1:9300/index

圖片描述

此處可以結(jié)合函數(shù)式編程的思想,將 index 的處理函數(shù)拿出來作為一個(gè)變量,代碼修改后如下所示

代碼塊
  • 1? package main
  • 2
  • 3? import (
  • 4? ? ? ? ? "net/http"
  • 5? )
  • 6
  • 7? func main() {
  • 8? ? ? ? ? http.HandleFunc("/index", index) //設(shè)置訪問的路由
  • 9? ? ? ? ? http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
  • 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? }

然后修改一下輸出字符串,使其輸出一個(gè)頁面,代碼修改后如下

代碼塊
  • 1? package main
  • 2
  • 3? import (
  • 4? ? ? ? "net/http"
  • 5? )
  • 6
  • 7? func main() {
  • 8? ? ? ? ?http.HandleFunc("/index", index) //設(shè)置訪問的路由
  • 9? ? ? ? ?http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
  • 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語言實(shí)戰(zhàn)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="請(qǐng)輸入賬號(hào)">
  • 27? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
  • 28? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
  • 29? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div>
  • 30? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div>
  • 31? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input type="password" class="form-control" id="password" name="password" placeholder="請(qǐng)輸入密碼">
  • 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? }

運(yùn)行上述代碼,然后再次在瀏覽器中輸入127.0.0.1:9300/index。

圖片描述

 

3. 配置頁面到 html

一般寫 web 應(yīng)用,會(huì)涉及到很多 html 文件,我們不可能將其全部都放在 Go 文件的字符串里,不方便調(diào)試的同時(shí)也影響代碼維護(hù)。所以我們一般會(huì)直接加載 html 文件

代碼示例:

代碼塊
  • 1? package main
  • 2
  • 3? import (
  • 4? ? ? ? ? "net/http"
  • 5? ? ? ? ? "text/template"
  • 6? )
  • 7
  • 8? func main() {
  • 9? ? ? ? ? http.HandleFunc("/index", index) //設(shè)置訪問的路由
  • 10? ? ? ? http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
  • 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? }

目錄結(jié)構(gòu)如下

圖片描述

index.html 的代碼如下:

代碼塊
  • 1? <!DOCTYPE html>
  • 2? <html>
  • 3
  • 4? <head>
  • 5? ? ? ? ? <meta charset="utf-8">
  • 6? ? ? ? ? <title>Go語言實(shí)戰(zhàn)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="請(qǐng)輸入賬號(hào)">
  • 15? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div>
  • 16? ? ? ? ? ? ? ? ? ? ? ? </div>
  • 17? ? ? ? ? ? ? ? ? ? ? ? <div>
  • 18? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div>
  • 19? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input type="password" id="password" name="password" placeholder="請(qǐng)輸入密碼">
  • 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>

執(zhí)行上述 Go 語言代碼,在瀏覽器中輸入127.0.0.1:9300/index

圖片描述

 

4. 數(shù)據(jù)傳輸

在 html 頁面點(diǎn)擊登錄暫時(shí)沒有任何反應(yīng),為了提交頁面到服務(wù)端,我們需要在服務(wù)端再編寫一個(gè)接收數(shù)據(jù)的路由,這個(gè)路由需要能夠接收 POST 請(qǐng)求。然后再這個(gè)路由中需要能驗(yàn)證賬號(hào)密碼是否正確,若是則跳轉(zhuǎn)到主頁,若不是則給出提示后跳轉(zhuǎn)到登錄頁。

代碼示例

代碼塊
  • 1? package main
  • 2
  • 3? import (
  • 4? ? ? ? ? "net/http"
  • 5? ? ? ? ? "text/template"
  • 6? )
  • 7
  • 8? func main() {
  • 9? ? ? ? ? http.HandleFunc("/index", index) //設(shè)置訪問的路由
  • 10
  • 11? ? ? ? http.HandleFunc("/check", check)
  • 12? ? ? ? http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
  • 13 ?}
  • 14
  • 15? func check(w http.ResponseWriter, r *http.Request) {
  • 16? ? ? ? if r.Method == "POST" {
  • 17? ? ? ? ? ? ? ? accountID := r.FormValue("username")//獲取賬號(hào)
  • 18? ? ? ? ? ? ? ? password := r.FormValue("password")//獲取密碼
  • 19? ? ? ? ? ? ? ? if accountID == "Codey" && password == "12345" {
  • 20? ? ? ? ? ? ? ? ? ? ? ? //跳轉(zhuǎn)到主頁
  • 21? ? ? ? ? ? ? ? ? ? ? ? t, _ := template.ParseFiles("view/home.html")
  • 22? ? ? ? ? ? ? ? ? ? ? ? t.Execute(w, nil)
  • 23? ? ? ? ? ? ? ? } else {
  • 24? ? ? ? ? ? ? ? ? ? ? ? //跳轉(zhuǎn)到登錄
  • 25? ? ? ? ? ? ? ? ? ? ? ? w.Write([]byte("<script>alert('賬號(hào)或者密碼不正確')</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語言實(shí)戰(zhàn)1</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)回登錄頁面

圖片描述

一個(gè)簡易的登錄功能就搭建完成了。

 

5. 小結(jié)

本文主要介紹了 Go 語言官方提供的 http 服務(wù),以及如何使用這個(gè)包來搭建一個(gè) web 應(yīng)用。其中需要注意區(qū)分前端發(fā)送過來的請(qǐng)求類型,POST 和 GET 兩個(gè)請(qǐng)求各自有各自的處理。

文章來源于網(wǎng)絡(luò),侵刪!

相關(guān)新聞

歷經(jīng)多年發(fā)展,已成為國內(nèi)好評(píng)如潮的Linux云計(jì)算運(yùn)維、SRE、Devops、網(wǎng)絡(luò)安全、云原生、Go、Python開發(fā)專業(yè)人才培訓(xùn)機(jī)構(gòu)!