自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 前端
Go語言寫Web應(yīng)用程序,涵蓋內(nèi)容:為載入和保存方法創(chuàng)建一個(gè)數(shù)據(jù)結(jié)構(gòu)體,引用http包來創(chuàng)建一個(gè)web應(yīng)用,引用template包來處理HTML模板,引用regexp包來驗(yàn)證用戶的輸入,引用閉包操作。

 介紹

涵蓋內(nèi)容:

◆ 為載入和保存方法創(chuàng)建一個(gè)數(shù)據(jù)結(jié)構(gòu)體

◆ 引用http包來創(chuàng)建一個(gè)web應(yīng)用

◆ 引用template包來處理HTML模板

◆ 引用regexp包來驗(yàn)證用戶的輸入

◆ 引用 閉包操作

可能涉及到的知識(shí):

◆ 設(shè)計(jì)經(jīng)驗(yàn)

◆ 明白基礎(chǔ)的web技術(shù)(HTTP,HTML)

◆ 一些UNIX命令行知識(shí)

從這里開始

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

  1. package main 
  2.  
  3. import ( 
  4.     "fmt" 
  5.     "io/ioutil" 
  6.     "os" 

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

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

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

  1. type Page struct { 
  2.     Title    string 
  3.     Body    []byte 

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

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

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

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

編輯頁面 模板e(cuò)idt.html

  1. <h1>Editing {{.Title |html}}</h1> 
  2.  
  3. <form action="/save/{{.Title |html}}" method="POST"> 
  4. <div><textarea name="body" rows="20" cols="80">{{printf "%s" .Body |html}}</textarea></div> 
  5. <div><input type="submit" value="Save"></div> 
  6. </form> 

查看頁面模板view.html

  1. <h1>{{.Title |html}}</h1> 
  2. <p>[<a href="/edit/{{.Title |html}}">edit</a>]</p> 
  3. <div>{{printf "%s" .Body |html}}</div> 

完整代碼:wiki.go

  1. package main 
  2.  
  3. import ( 
  4.     "http" 
  5.     "io/ioutil" 
  6.     "os" 
  7.     "regexp" 
  8.     "template" 
  9.  
  10. type Page struct { 
  11.     Title string 
  12.     Body  []byte 
  13.  
  14. func (p *Page) save() os.Error { 
  15.     filename :p.Title + ".txt" 
  16.     return ioutil.WriteFile(filename, p.Body, 0600) 
  17.  
  18. func loadPage(title string) (*Page, os.Error) { 
  19.     filename :title + ".txt" 
  20.     body, err :ioutil.ReadFile(filename) 
  21.     if err != nil { 
  22.         return nil, err 
  23.     } 
  24.     return &Page{Title: title, Body: body}, nil 
  25.  
  26. func viewHandler(w http.ResponseWriter, r *http.Request, title string) { 
  27.     p, err :loadPage(title) 
  28.     if err != nil { 
  29.         http.Redirect(w, r, "/edit/"+title, http.StatusFound) 
  30.         return 
  31.     } 
  32.     renderTemplate(w, "view", p) 
  33.  
  34. func editHandler(w http.ResponseWriter, r *http.Request, title string) { 
  35.     p, err :loadPage(title) 
  36.     if err != nil { 
  37.         p = &Page{Title: title} 
  38.     } 
  39.     renderTemplate(w, "edit", p) 
  40.  
  41. func saveHandler(w http.ResponseWriter, r *http.Request, title string) { 
  42.     body :r.FormValue("body") 
  43.     p := &Page{Title: title, Body: []byte(body)} 
  44.     err :p.save() 
  45.     if err != nil { 
  46.         http.Error(w, err.String(), http.StatusInternalServerError) 
  47.         return 
  48.     } 
  49.     http.Redirect(w, r, "/view/"+title, http.StatusFound) 
  50.  
  51. var templates = make(map[string]*template.Template) 
  52.  
  53. func init() { 
  54.     for _, tmpl :range []string{"edit", "view"} { 
  55.         t :template.Must(template.ParseFile(tmpl + ".html")) 
  56.         templates[tmpl] = t 
  57.     } 
  58.  
  59. func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { 
  60.     err :templates[tmpl].Execute(w, p) 
  61.     if err != nil { 
  62.         http.Error(w, err.String(), http.StatusInternalServerError) 
  63.     } 
  64.  
  65. const lenlenPath = len("/view/") 
  66.  
  67. var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$") 
  68.  
  69. func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { 
  70.     return func(w http.ResponseWriter, r *http.Request) { 
  71.         title :r.URL.Path[lenPath:] 
  72.         if !titleValidator.MatchString(title) { 
  73.             http.NotFound(w, r) 
  74.             return 
  75.         } 
  76.         fn(w, r, title) 
  77.     } 
  78.  
  79. func main() { 
  80.     http.HandleFunc("/view/", makeHandler(viewHandler)) 
  81.     http.HandleFunc("/edit/", makeHandler(editHandler)) 
  82.     http.HandleFunc("/save/", makeHandler(saveHandler)) 
  83.     http.ListenAndServe(":8080", nil) 

運(yùn)行測試:

$ 8g wiki.go

$ 8l wiki.8

$ ./8.out

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

效果圖:

 

 

 

 

 

原文:http://www.cnblogs.com/zitsing/archive/2012/03/19/2406226.html

【編輯推薦】

  1. Go語言源碼可追溯到1972 年?
  2. 谷歌發(fā)布Go編程語言***候選版
  3. 用Google Go語言實(shí)現(xiàn)http共享
  4. Google Web App開發(fā)指南之構(gòu)建優(yōu)秀的Web Apps
  5. Google Web App開發(fā)指南:交互設(shè)計(jì)

 

責(zé)任編輯:陳貽新 來源: 傾城
相關(guān)推薦

2024-01-02 00:18:56

Buffalo項(xiàng)目Go Web框架

2024-01-15 00:42:55

Go語言應(yīng)用程序

2010-05-20 09:48:36

2011-03-22 14:12:17

LAMP

2010-03-09 13:27:23

Web 2.0應(yīng)用程序

2023-01-09 17:04:24

2010-02-01 14:05:03

2009-04-01 14:33:33

2009-07-09 16:47:26

Servlet的Web

2012-06-11 09:37:41

2013-08-08 09:48:10

Web

2013-11-19 15:35:01

2011-04-01 11:01:02

應(yīng)用程序BlackBerryJava

2012-04-19 09:34:21

ibmdw

2019-02-11 09:35:04

Python應(yīng)用程序Tornado

2009-01-19 11:07:42

C#Web.NET

2010-11-18 09:32:19

微軟開源Web應(yīng)用程序

2009-01-16 09:22:40

Web應(yīng)用程序Web程序管理Web服務(wù)

2009-07-29 10:30:53

Web應(yīng)用程序ASP.NET

2009-08-27 11:40:43

ibmdw云計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)