1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Go語言怎樣寫Web應(yīng)用程序

      這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Go語言怎樣寫Web應(yīng)用程序,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

      成都創(chuàng)新互聯(lián)是一家專業(yè)提供浚縣企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為浚縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

      從這里開始

      你要有一個可以運(yùn)行Go語言的計算機(jī)或虛擬機(jī),怎么樣安裝Go,請參考安裝Go教程。首先創(chuàng)建一個目錄,在目錄下創(chuàng)建一個wiki.go文件,用你喜歡的編輯器打開并輸入以下內(nèi)容:

      package main  import (     "fmt"     "io/ioutil"     "os" )

      這fmt,ioutil和os都是go語言的標(biāo)準(zhǔn)庫,一會我將增加其他方法和更多的包。

      數(shù)據(jù)結(jié)構(gòu)

      讓我們聲明一個數(shù)據(jù)結(jié)構(gòu),這個結(jié)構(gòu)主要包含兩個字段,一個是標(biāo)題,一個是內(nèi)容。

      type Page struct {     Title    string     Body    []byte }

      接下來,我們給Page 這個結(jié)構(gòu)體寫個保存方法,代碼如下:

      func (p *Page) save() os.Error {     filename := p.Title + ".txt"     return ioutil.WriteFile(filename, p.Body, 0600) }

      這個方法的簽名是:接收一個Page結(jié)構(gòu)體指針,返回一個os.Error錯誤。

      在一下的代碼中還是用了http包和模板包,具體內(nèi)容參考具體代碼,再這里就不詳細(xì)貼出來了。下面是模板內(nèi)容,把他們放到wiki.go同一目錄下。

      編輯頁面 模板eidt.html

      Editing {{.Title |html}}

         
      {{printf "%s" .Body |html}}
       
       

      查看頁面模板view.html

      1. {{.Title |html}}

         

      2. [edit]

         

      3. {{printf "%s" .Body |html}}
         

      完整代碼:wiki.go

      package main  import (     "http"     "io/ioutil"     "os"     "regexp"     "template" )  type Page struct {     Title string     Body  []byte }  func (p *Page) save() os.Error {     filename := p.Title + ".txt"     return ioutil.WriteFile(filename, p.Body, 0600) }  func loadPage(title string) (*Page, os.Error) {     filename := title + ".txt"     body, err := ioutil.ReadFile(filename)     if err != nil {         return nil, err     }     return &Page{Title: title, Body: body}, nil }  func viewHandler(w http.ResponseWriter, r *http.Request, title string) {     p, err := loadPage(title)     if err != nil {         http.Redirect(w, r, "/edit/"+title, http.StatusFound)         return     }     renderTemplate(w, "view", p) }  func editHandler(w http.ResponseWriter, r *http.Request, title string) {     p, err := loadPage(title)     if err != nil {         p = &Page{Title: title}     }     renderTemplate(w, "edit", p) }  func saveHandler(w http.ResponseWriter, r *http.Request, title string) {     body := r.FormValue("body")     p := &Page{Title: title, Body: []byte(body)}     err := p.save()     if err != nil {         http.Error(w, err.String(), http.StatusInternalServerError)         return     }     http.Redirect(w, r, "/view/"+title, http.StatusFound) }  var templates = make(map[string]*template.Template)  func init() {     for _, tmpl := range []string{"edit", "view"} {         t := template.Must(template.ParseFile(tmpl + ".html"))         templates[tmpl] = t     } }  func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {     err := templates[tmpl].Execute(w, p)     if err != nil {         http.Error(w, err.String(), http.StatusInternalServerError)     } }  const lenlenPath = len("/view/")  var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")  func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {     return func(w http.ResponseWriter, r *http.Request) {         title := r.URL.Path[lenPath:]         if !titleValidator.MatchString(title) {             http.NotFound(w, r)             return         }         fn(w, r, title)     } }  func main() {     http.HandleFunc("/view/", makeHandler(viewHandler))     http.HandleFunc("/edit/", makeHandler(editHandler))     http.HandleFunc("/save/", makeHandler(saveHandler))     http.ListenAndServe(":8080", nil) }

      運(yùn)行測試:

      $ 8g wiki.go

      $ 8l wiki.8

      $ ./8.out

      在地址欄輸入地址:http://localhost:8080/view/aNewPage

      效果圖:

      Go語言怎樣寫Web應(yīng)用程序
      Go語言怎樣寫Web應(yīng)用程序

      上述就是小編為大家分享的Go語言怎樣寫Web應(yīng)用程序了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


      新聞標(biāo)題:Go語言怎樣寫Web應(yīng)用程序
      標(biāo)題網(wǎng)址:http://ef60e0e.cn/article/pdhoph.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        贵德县| 安溪县| 文成县| 昭觉县| 循化| 镇坪县| 崇义县| 永康市| 大埔县| 区。| 南京市| 镇赉县| 博乐市| 兰坪| 白玉县| 阜南县| 辛集市| 洛南县| 安庆市| 双江| 汝城县| 凌海市| 濮阳县| 清水县| 巩义市| 昭平县| 塘沽区| 青阳县| 大港区| 徐水县| 龙江县| 泰兴市| 宜丰县| 福清市| 克什克腾旗| 伊川县| 昭觉县| 东丰县| 克什克腾旗| 都兰县| 新巴尔虎左旗|