1.先看一下整個結(jié)構(gòu):
創(chuàng)新互聯(lián)建站專注于濟水街道企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,商城建設。濟水街道網(wǎng)站建設公司,為濟水街道等地區(qū)提供建站服務。全流程按需設計網(wǎng)站,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務

主要是web和html目錄,分別存放go代碼和html相關的資源文件。
2.html代碼比較簡單,代碼如下:
<html> <head> <title>Go web</title> </head> <body> <img src="/html/pics/girl.jpg" width="500" height="500"> <form action="http://127.0.0.1:8080/login" method="post"> 用戶名:<input type="text" name="username"> 密碼:<input type="password" name="password"> <input type="submit" value="登陸"> </form> </body> </html>
就是顯示一張圖片,然后加登陸表單。
3.而go代碼也比較簡單,如下:
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
func login(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.Method == "GET" {
t, err := template.ParseFiles("html/login.html")
if err != nil {
fmt.Fprintf(w, "parse template error: %s", err.Error())
return
}
t.Execute(w, nil)
} else {
username := r.Form["username"]
password := r.Form["password"]
fmt.Fprintf(w, "username = %s, password = %s", username, password)
}
}
func main() {
http.HandleFunc("/html/pics/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
http.HandleFunc("/login", login)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}主要是注意顯示圖片的路徑,不能是原來的html的路徑,必須是go認識的路徑,所以圖片的位置也設置了路由,見http.ServeFile方法,并注意html設置的圖片路徑。
以上這篇golang解析html網(wǎng)頁的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。
分享題目:golang解析html網(wǎng)頁的方法
文章源于:http://chinadenli.net/article16/jhjpgg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、做網(wǎng)站、營銷型網(wǎng)站建設、自適應網(wǎng)站、域名注冊、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)