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 ") Func Main () {http. Handle ("/images/", http. Fileserver (http. Dir ("asset"))) HTTP. Handlefunc ("/", Listhandler) http. Handlefunc ("/add", AddHandler) http. Listenandserve (": 8055", nil)}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 failed!</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: [Email protected]
blog:http://blog.csdn.net/xcl168
Go language implementation Simple message book