​
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"text/template"
)

func Welcome() string {
	return "Welcome"
}
func Doing(name string) string {
	return name + ", Learing Go web template"
}
func SayHello(w http.ResponseWriter, r *http.Request) {
	htmlByte, err := ioutil.ReadFile("funcs.html")
	if err != nil {
		fmt.Println("Read html failed,err:", err)
		return
	}
	loveGo := func() string {
		return "欢迎一起学习《Go web编程实战派——从入门到精通》"
	}
	tmpl1, err1 := template.New("funcs").Funcs(template.FuncMap{"loveGo": loveGo}).Parse(string(htmlByte))
	if err1 != nil {
		fmt.Println("Create template failed,err1:", err1)
		return
	}
	funcMap := template.FuncMap{
		"Welcome": Welcome,
		"Doing":   Doing,
	}
	name := "Shirdon"
	tmpl2, err2 := template.New("test").Funcs(funcMap).Parse("{{Welcome}}\n{{Doing .}}\n")
	if err2 != nil {
		panic(err2)
	}
	fmt.Println(tmpl1.Tree)
	tmpl1.Execute(w, name)
	tmpl2.Execute(w, name)
}
func main() {
	http.Handle("/", http.HandlerFunc(SayHello))
	http.ListenAndServe(":8087", nil)

}

​

由于原书错误太多,只好改一下发到这里,虽然不是太大的问题,但是感觉还是不够严谨