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

全部開發(fā)者教程
首頁 慕課教程 Go 入門教程 Go 入門教程 46 使用 Go 語言搭建簡易登錄功能

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

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

1. 搭建服務

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

代碼示例:

package main

import (
    "net/http"
)

func main() {
    http.ListenAndServe("127.0.0.1:9300", nil) //設(shè)置監(jiān)聽的端口
}

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

圖片描述

2. 編寫路由

服務已經(jīng)可以運行了,接下來就是要編寫能被外部訪問的路由接口,http 請求分為兩種,POST 請求和 GET 請求。我們首先想實現(xiàn)的是一個網(wǎng)站登錄頁面打開的路由 /index,需要編寫一個能響應 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ù)拿出來作為一個變量,代碼修改后如下所示

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>"))
    }
} 

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

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語言實戰(zhàn)1</title>
        </head>
        <body>
            <div>
                <h3>登錄</h3>
                <form>
                    <div>
                        <div>
                            <input type="text"  id="username" name="username" placeholder="請輸入賬號">
                        </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>`))
    }
}

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

圖片描述

3. 配置頁面到 html

一般寫 web 應用,會涉及到很多 html 文件,我們不可能將其全部都放在 Go 文件的字符串里,不方便調(diào)試的同時也影響代碼維護。所以我們一般會直接加載 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語言實戰(zhàn)1</title>
</head>
<body>
    <div>
        <h3>登錄</h3>
        <form>
            <div>
                <div>
                    <input type="text"  id="username" name="username" placeholder="請輸入賬號">
                </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 頁面點擊登錄暫時沒有任何反應,為了提交頁面到服務端,我們需要在服務端再編寫一個接收數(shù)據(jù)的路由,這個路由需要能夠接收 POST 請求。然后再這個路由中需要能驗證賬號密碼是否正確,若是則跳轉(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")//獲取賬號
        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('賬號或者密碼不正確')</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語言實戰(zhàn)1</title>
</head>
<body>
    <div>
        <h3>主頁</h3>
       這里是主頁
    </div>
</body>
</html>

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

圖片描述

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

圖片描述

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

圖片描述

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

圖片描述

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

圖片描述

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

5. 小結(jié)

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