GO Web编程---网上书店(1)
完成項目后的目錄結(jié)構(gòu):
完成項目后的源碼:
1、
安裝go環(huán)境
我是Mac環(huán)境,具體步驟,略
[復(fù)雜一點的這里是go mod相關(guān)配置]
2、IDE:Goland
選擇go models,因為它會自動創(chuàng)建go mod,之后的項目里用的包都放在這里,很方便,比gopath要好用
下載html模版,之后的幾天里都會用到,我是結(jié)合視頻做的
b站鏈接
如果看我寫的貼子運行對不上的話,去看視頻,雖然很慢
鏈接:https://pan.baidu.com/s/16N7F0ZlF5MLeaFE_WWVNOA
提取碼:t3p4
解壓之后,把day1里的views文件夾復(fù)制到goland里
接著開始寫main.go
package mainimport ("fmt""html/template""net/http" )func IndexHandler (w http.ResponseWriter, r *http.Request) {t := template.Must(template.ParseFiles("views/index.html"))t.Execute(w, "") }func main() {http.HandleFunc("/main", IndexHandler)fmt.Println("在監(jiān)聽:http://localhost:8080")err := http.ListenAndServe(":8080", nil)if err != nil {panic(err)} }詳細(xì)解釋一下,對于小白來說
首先第一句
:http.HandleFunc(“/main”, IndexHandler)
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))HandleFunc注冊一個處理器函數(shù)handler和對應(yīng)的模式pattern(注冊到DefaultServeMux)。就是說你點開\main結(jié)尾的網(wǎng)址,http://localhost:8080\main ,處理你的請求的處理機(jī)就是IndexHandler,那么處理機(jī)是什么呢?大概相當(dāng)于電腦之于CPU,瀏覽器之于IndexHandler
第二句:
err := http.ListenAndServe(“:8080”, nil)
if err != nil {
panic(err)
}
ListenAndServe監(jiān)聽TCP地址addr “:8080”,使用handler參數(shù)調(diào)用Serve函數(shù)處理接收到的連接。handler參數(shù)一般會設(shè)為nil,此時會使用DefaultServeMux。
DefaultServeMux的意思是用包里自己帶的多路復(fù)用器,這個復(fù)用器也可以自己寫,那參數(shù)nil就要改成你寫的那個,咱們就直接默認(rèn),不搞花里胡哨
看第三句:
func IndexHandler (w http.ResponseWriter, r *http.Request) {}
這是一個處理器,http.ResponseWrite是回應(yīng)報文之類的東西,我不太懂,http.Request就是請求報文了。
經(jīng)常性的用法就是在http.Request里獲取很多很多數(shù)據(jù),表單吶文件吶,反正請求報文里邊的東西都可以取出來,用戶在瀏覽器提交的東西靠它來獲取到代碼里。
http.ResponseWrite就可以寫東西進(jìn)去作為回應(yīng)報文給客戶端咯。
第四句:
t := template.Must(template.ParseFiles(“views/index.html”))
t.Execute(w, “”)
這就要講到html/template庫了,看意思,template,樣板; 模板。
它可以將html模版解析出來,解析出來之后呢,你就可以對他修改之類的操作,
比如向html文件傳幾個值然后顯示在網(wǎng)站上啊,就像一個頁面,要你輸入用戶名密碼,然后通過表單,你得到了這個用戶名密碼,你要把它顯示在新的頁面上,比如index.html,你就得先解析這個模版,然后傳上值,傳值用t.Execute(w,
“要傳的值”)
外面加上template.Must()
func Must(t *Template, err error) *Template Must函數(shù)用于包裝返回(*Template, error)的函數(shù)/方法調(diào)用,它會在err非nil時panic,一般用于變量初始化:就是加上一個報錯功能,沒什么值得注意的地方,就是更安全了
func (t *Template) Execute(wr io.Writer, data interface{}) error Execute方法將解析好的模板應(yīng)用到data上,并將輸出寫入wr。如果執(zhí)行時出現(xiàn)錯誤,會停止執(zhí)行,但有可能已經(jīng)寫入wr部分?jǐn)?shù)據(jù)。模板可以安全的并發(fā)執(zhí)行。好了,講解完畢。看一下結(jié)果
http://localhost:8080\main
成功!
然后發(fā)現(xiàn)有幾張圖片沒有顯示,其實這是因為http請求內(nèi)部資源的時候,獲取不到,比如這個圖片
<img class="book_img" alt="" src="/static/img/default.jpg" />路徑是/static/img/default.jpg
得加上這句代碼,加在func main 第一句:
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("views/static"))))把http訪問的/static/,而FileServer 查詢 views/static,因為我們文件夾里圖片的路徑就是 views/static/img/default.jpg,直接找/static/img/default.jpg肯定找不到的。
func Handle(pattern string, handler Handler) Handle注冊HTTP處理器handler和對應(yīng)的模式pattern(注冊到DefaultServeMux)。如果該模式已經(jīng)注冊有一個處理器,Handle會panic。ServeMux的文檔解釋了模式的匹配機(jī)制。 func StripPrefix(prefix string, h Handler) Handler StripPrefix返回一個處理器,該處理器會將請求的URL.Path字段中給定前綴prefix去除后再交由h處理。StripPrefix會向URL.Path字段中沒有給定前綴的請求回復(fù)404 page not found。 func FileServer(root FileSystem) Handler FileServer返回一個使用FileSystem接口root提供文件訪問服務(wù)的HTTP處理器。要使用操作系統(tǒng)的FileSystem接口實現(xiàn),可使用http.Dir: http.Handle("/", http.FileServer(http.Dir("/tmp")))Example // Simple static webserver: log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))所以上面那句話的意思就是:
如果http請求字段出現(xiàn)了static,那么交給http.StripPrefix(“/static/”,http.FileServer(http.Dir(“views/static”)))這個處理機(jī)處理,
處理機(jī)http.StripPrefix(“/static/”, handler),把/static/去掉,那么就剩空字符串 “” 了,交給
http.FileServer(http.Dir(“views/static”))這個處理機(jī)處理,
最后,""的pattern由http.FileServe最后處理成訪問"views/static"的文件
就是說,找\static\最后找到了文件\views\static
現(xiàn)在就可以看到圖片了
然后發(fā)現(xiàn)點開登陸/注冊, 購物車,后臺管理都沒有反應(yīng),打開index.html,發(fā)現(xiàn)跳轉(zhuǎn)頁面
<a href="pages/user/login.html">登錄</a> | <a href="pages/user/regist.html">注冊</a> <a href="/pages/cart/cart.html">購物車</a><a href="/pages/manager/manager.html">后臺管理</a>pages/user/login.html,這個pages跟上面那個static一個意思,繼續(xù)加上代碼
http.Handle("/pages/", http.StripPrefix("/pages/", http.FileServer(http.Dir("views/pages"))))重新編譯,點開登陸,
成功!!!
還會繼續(xù)更新!
總結(jié)
以上是生活随笔為你收集整理的GO Web编程---网上书店(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原型链 继承
- 下一篇: 介绍5款非常棒的移动自动化测试工具