This is a creation in Article, where the information may have evolved or changed.
Made a simple message to practice HTTP and template.
Main Go code:
Package Main//golang version of the message//author:xiong Chuan liang//date:2015-3-2import ("FMT" "Html/template" "Io/ioutil" "net/ HTTP "" OS "" Log ") func Main () {http. Handle ("/images/", http. Fileserver (http. Dir ("asset"))) HTTP. Handlefunc ("/", Makehandlefunc (Listhandler)) http. Handlefunc ("/add", Makehandlefunc (AddHandler)) if err: = http. Listenandserve (": 8055", nil); Err! = Nil {log. Fatal ("Listenandserve:", err)}}type handlefunctype func (http. Responsewriter, *http. Request) func Makehandlefunc (f handlefunctype) (Handlefunctype) {return func (w http. Responsewriter,r *http. Request) {defer func () {if x: = Recover (); x!= Nil {log. Printf ("[%v] Panic:%v", R.remoteaddr,x)}} () f (w,r)}}func AddHandler (w http. Responsewriter, R *http. Request) {h, _: = template. Parsefiles ("Template/addinfo.tpl", "Template/header.tpl", "Template/footer.tpl") h.executetemplate (W, "Addinfo", Nil)}func Listhandler (w http. Responsewriter, R *http. Request) {if R.method = = "POST" {r.parseform () If Len (r.form["note"][0]) = = 0 {infohandler (W, R, ' <b> commit lost!</b> <br/> message can not be empty! <br/> <a href= "Http://127.0.0.1:8055/add" > Return </a> ') Return}info: = Fmt. Sprintf ("Message person: <a href= ' mailto:%s ' >%s</a><br/> message:%s
Its GUESTBOOK.TPL:
{{define "Guestbook"}}
The effect of the implementation is as follows:
The whole source package is here: Click to download
Mail:xcl_168@aliyun.com
blog:http://blog.csdn.net/xcl168