博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Go语言写Web 应用程序
阅读量:4365 次
发布时间:2019-06-07

本文共 3406 字,大约阅读时间需要 11 分钟。

介绍

涵盖内容:
  • 为载入和保存方法创建一个数据结构体
  • 引用http包来创建一个web应用
  • 引用template包来处理HTML模板
  • 引用regexp包来验证用户的输入
  • 引用 闭包操作
可能涉及到的知识:
  • 设计经验
  • 明白基础的web技术(HTTP,HTML)
  • 一些UNIX命令行知识

从这里开始

你要有一个可以运行Go语言的计算机或虚拟机,怎么样安装Go,请参考安装Go教程。首先创建一个目录,在目录下创建一个wiki.go文件,用你喜欢的编辑器打开并输入以下内容:
package main import ( "fmt" "io/ioutil" "os" )
 
这fmt,ioutil和os都是go语言的标准库,一会我将增加其他方法和更多的包。
数据结构
让我们声明一个数据结构,这个结构主要包含两个字段,一个是标题,一个是内容。
type Page struct {
Title string Body []byte }
 
接下来,我们给Page 这个结构体写个保存方法,代码如下:
func (p *Page) save() os.Error {
filename := p.Title + ".txt" return ioutil.WriteFile(filename, p.Body, 0600) }
 
这个方法的签名是:接收一个Page结构体指针,返回一个os.Error错误。
在一下的代码中还是用了http包和模板包,具体内容参考具体代码,再这里就不详细贴出来了。下面是模板内容,把他们放到wiki.go同一目录下。

编辑页面 模板eidt.html

Editing {
{.Title |html}}

 

查看页面模板view.html

{
{.Title |html}}

[edit]

{
{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 lenPath = 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) }
 

运行测试:

$ 8g wiki.go$ 8l wiki.8$ ./8.out
在地址栏输入地址:http://localhost:8080/view/aNewPage

效果图:

转载于:https://www.cnblogs.com/zitsing/archive/2012/03/19/2406226.html

你可能感兴趣的文章
利用CSS、JavaScript及Ajax实现图片预加载的三大方法
查看>>
js时间戳转时间格式
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Linux的用户态和内核态
查看>>
JavaScript原生错误及检测
查看>>
最小权限的挑战
查看>>
jquery 视觉特效(水平滚动图片)
查看>>
SVG笔记
查看>>
linux下使用dd命令写入镜像文件到u盘
查看>>
前端必读:浏览器内部工作原理
查看>>
单例对象的创建与销毁
查看>>
Beta 冲刺(1/7)
查看>>
修改 Vultr 登录密码
查看>>
CSS学习
查看>>
Centos 安装lnmp完整版
查看>>
【转】Eclipse和PyDev搭建完美Python开发环境(Ubuntu篇)
查看>>
Differences between page and segment
查看>>
字符串之strcmp
查看>>
最长公共子序列(不连续)
查看>>
微服务:Java EE的拯救者还是掘墓人?
查看>>