Go语言写Web应用程序

开发 前端
Go语言写Web应用程序,涵盖内容:为载入和保存方法创建一个数据结构体,引用http包来创建一个web应用,引用template包来处理HTML模板,引用regexp包来验证用户的输入,引用闭包操作。

 介绍

涵盖内容:

◆ 为载入和保存方法创建一个数据结构体

◆ 引用http包来创建一个web应用

◆ 引用template包来处理HTML模板

◆ 引用regexp包来验证用户的输入

◆ 引用 闭包操作

可能涉及到的知识:

◆ 设计经验

◆ 明白基础的web技术(HTTP,HTML)

◆ 一些UNIX命令行知识

从这里开始

你要有一个可以运行Go语言的计算机或虚拟机,怎么样安装Go,请参考安装Go教程。首先创建一个目录,在目录下创建一个wiki.go文件,用你喜欢的编辑器打开并输入以下内容:

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

这fmt,ioutil和os都是go语言的标准库,一会我将增加其他方法和更多的包。

数据结构

让我们声明一个数据结构,这个结构主要包含两个字段,一个是标题,一个是内容。

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

接下来,我们给Page 这个结构体写个保存方法,代码如下:

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

这个方法的签名是:接收一个Page结构体指针,返回一个os.Error错误。

在一下的代码中还是用了http包和模板包,具体内容参考具体代码,再这里就不详细贴出来了。下面是模板内容,把他们放到wiki.go同一目录下。

编辑页面 模板eidt.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) 

运行测试:

$ 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. 谷歌发布Go编程语言***候选版
  3. 用Google Go语言实现http共享
  4. Google Web App开发指南之构建优秀的Web Apps
  5. Google Web App开发指南:交互设计

 

责任编辑:陈贻新 来源: 倾城
相关推荐

2024-01-02 00:18:56

Buffalo项目Go Web框架

2024-01-15 00:42:55

Go语言应用程序

2010-05-20 09:48:36

2011-03-22 14:12:17

LAMP

2010-03-09 13:27:23

Web 2.0应用程序

2023-01-09 17:04:24

2009-07-09 16:47:26

Servlet的Web

2009-04-01 14:33:33

2012-06-11 09:37:41

2010-02-01 14:05:03

2013-08-08 09:48:10

Web

2013-11-19 15:35:01

2011-04-01 11:01:02

应用程序BlackBerryJava

2012-04-19 09:34:21

ibmdw

2009-01-19 11:07:42

C#Web.NET

2009-07-29 10:30:53

Web应用程序ASP.NET

2019-02-11 09:35:04

Python应用程序Tornado

2009-01-16 09:22:40

Web应用程序Web程序管理Web服务

2010-11-18 09:32:19

微软开源Web应用程序

2009-08-27 11:40:43

ibmdw云计算
点赞
收藏

51CTO技术栈公众号