第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

全部開發(fā)者教程

Go 入門教程

Go 語言基礎(chǔ)教程
01 Go語言簡(jiǎn)介 02 Go 在 Windows 上的安裝及配置 03 Go 在 Linux 上的安裝與配置 04 Go 在 MacOS 上的安裝及配置 05 Git 的下載和安裝 06 VSCode 編輯器安裝和配置 07 Go 的變量聲明 08 Go 的變量賦值 09 Go 語言的 := 10 Go 的整型(int) 11 Go 的無符號(hào)整型(uint) 12 Go 的浮點(diǎn)型(float) 13 Go 的字符類型 14 Go 的布爾型(bool) 15 Go 的強(qiáng)制類型轉(zhuǎn)換 16 Go 語言的常量 17 Go 語言中代替枚舉的辦法 18 Go 語言中的運(yùn)算符 19 Go 語言的分支語句 20 Go 語言的循環(huán)語句 21 Go 語言的通道 22 Go 語言中的函數(shù) 23 Go 語言函數(shù)中的 defer 24 Go 語言中的閉包 25 Go 語言的指針 26 Go 語言中的數(shù)組 27 Go 語言中的切片 28 Go 語言中的 Map 29 Go 語言中的 range 30 Go 語言拓展以有類型 31 Go 語言中的結(jié)構(gòu)體和"面向?qū)ο?quot; 32 Go 語言中的接口 33 Go 語言類型的內(nèi)嵌 34 Go 語言中的 nil 35 Go 語言函數(shù)式編程 36 Go 語言中的錯(cuò)誤和異常處理 37 Go 語言的并發(fā) 38 Go 語言中的包 39 Go 語言 go mod 包依賴管理工具 40 Go 語言的文件操作 41 Go 語言中的系統(tǒng)包 42 Go 語言的 strings 系統(tǒng)包的使用 43 Go 語言中的變參 44 Go 語言中的反射 45 Go 語言性能測(cè)試 46 使用 Go 語言搭建簡(jiǎn)易登錄功能 47 使用 gin 包優(yōu)化登錄功能
首頁 慕課教程 Go 入門教程 Go 入門教程 46 使用 Go 語言搭建簡(jiǎn)易登錄功能

使用 Go 語言搭建簡(jiǎn)易登錄功能

在這篇文章之前,已經(jīng)學(xué)完了 Go 語言所有基礎(chǔ)特性,對(duì) 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 請(qǐng)求分為兩種,POST 請(qǐng)求和 GET 請(qǐng)求。我們首先想實(shí)現(xiàn)的是一個(gè)網(wǎng)站登錄頁面打開的路由 /index,需要編寫一個(gè)能響應(yīng) GET 請(qǐng)求的路由。

代碼示例:

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="請(qǐng)輸入賬號(hào)">
                        </div>
                    </div>
                    <div>
                        <div>
                            <input type="password" class="form-control" id="password" name="password" placeholder="請(qǐng)輸入密碼">
                        </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="請(qǐng)輸入賬號(hào)">
                </div>
            </div>
            <div>
                <div>
                    <input type="password" id="password" name="password" placeholder="請(qǐng)輸入密碼">
                </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 請(qǐng)求。然后再這個(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è)簡(jiǎn)易的登錄功能就搭建完成了。

5. 小結(jié)

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