---
title: "Go Web 开发（二）"
author: "ihuashao"
date: "2011-03-10T22:36:00Z"
tags: ["Go", "HTML", "Web", "编程"]
slug: "go-web"
draft: false
lastmod: "2011-03-10T22:36:00Z"
id: 955749
url: "https://jishudaima.iteye.com/blog/955749"
---

## 目的

了解使用 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}

## 总结

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

## 下一步

- 数据持久化

本文是使用 [B3log Solo](http://b3log-solo.googlecode.com/) 从 [简约设计の艺术](http://88250.b3log.org:80) 进行同步发布的

原文地址：[http://88250.b3log.org/go-web-2.html](http://88250.b3log.org/go-web-2.html)