yzd 发表于 2013-2-7 20:52:07

Go Web 开发(二)

目的

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

http://88250.b3log.org/datastore-file-access.do?oId=1299689200847
http://88250.b3log.org/datastore-file-access.do?oId=1299767321684
代码

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}

总结


[*]自动解析类型字段
[*]模板需要自行编程进行缓存
下一步


[*]数据持久化
页: [1]
查看完整版本: Go Web 开发(二)