ITeye博客

Go Web 开发(二)

ihuashao · 2011-03-10 22:36 · HTML · Go · 编程 · Web · Markdown

目的

了解使用 Go 模板技术开发 Web 应用的。

效果

代码

server.go

package main

import (
"fmt"
"http"
"template"
)

type User struct {
Name string
}

func Register(w http.ResponseWriter, r *http.Request) {
if "GET" == r.Method {
fmt.Fprintln(w, "

Register

" +
"

" +
" User Name: " +
"

")
return
}

user := &User{r.FormValue("userName")}
t, _ := template.ParseFile("hello.html", nil)
t.Execute(w, user)
}

func main() {
http.HandleFunc("/", Register)
http.ListenAndServe(":8080", nil)
}

hello.html

Hello, {Name}

总结

  • 自动解析类型字段
  • 模板需要自行编程进行缓存

下一步

  • 数据持久化

本文是使用 B3log Solo 从 简约设计の艺术 进行同步发布的

原文地址:http://88250.b3log.org/go-web-2.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。