Go语言实现简单的留言本

前端之家收集整理的这篇文章主要介绍了Go语言实现简单的留言本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

做了个简单的留言本用来练习http与template.


主Go代码:

  1. package main
  2.  
  3. //Golang版本的留言本
  4. //author:Xiong Chuan Liang
  5. //date:2015-3-2
  6.  
  7. import (
  8. "fmt"
  9. "html/template"
  10. "io/IoUtil"
  11. "net/http"
  12. "os"
  13. "log"
  14. )
  15.  
  16. func main() {
  17. http.Handle("/images/",http.FileServer(http.Dir("asset")))
  18.  
  19. http.HandleFunc("/",makeHandleFunc(listHandler))
  20. http.HandleFunc("/add",makeHandleFunc(addHandler))
  21.  
  22. if err := http.ListenAndServe(":8055",nil); err != nil {
  23. log.Fatal("ListenAndServe: ",err)
  24. }
  25. }
  26.  
  27.  
  28. type HandleFuncType func(http.ResponseWriter,*http.Request)
  29.  
  30. func makeHandleFunc(f HandleFuncType)(HandleFuncType){
  31. return func(w http.ResponseWriter,r *http.Request){
  32. defer func(){
  33. if x := recover(); x!= nil {
  34. log.Printf("[%v] panic: %v",r.RemoteAddr,x)
  35. }
  36. }()
  37. f(w,r)
  38. }
  39. }
  40.  
  41.  
  42. func addHandler(w http.ResponseWriter,r *http.Request) {
  43. h,_ := template.ParseFiles("template/addinfo.tpl","template/header.tpl","template/footer.tpl")
  44. h.ExecuteTemplate(w,"addinfo",nil)
  45. }
  46.  
  47. func listHandler(w http.ResponseWriter,r *http.Request) {
  48.  
  49. if r.Method == "POST" {
  50. r.ParseForm()
  51. if len(r.Form["note"][0]) == 0 {
  52. infoHandler(w,r,`<b>提交失败!</b> <br/>留言不能为空! <br/> <a href="http://127.0.0.1:8055/add">返回</a>`)
  53. return
  54. }
  55. info := fmt.Sprintf("留言人:<a href='mailto:%s'>%s</a><br/>留言:%s<hr>",r.FormValue("email"),//r.Form.Get("email")
  56. r.FormValue("nickname"),r.FormValue("note"))
  57. writeInfo(info)
  58. }
  59.  
  60. list,_ := readInfo()
  61. msg := map[string]template.HTML{"List": template.HTML(list)}
  62.  
  63. h,_ := template.ParseFiles("template/guestbook.tpl","guestbook",msg)
  64. }
  65.  
  66. func infoHandler(w http.ResponseWriter,r *http.Request,info string) {
  67. var base = `
  68. <!DOCTYPE html>
  69. <html>
  70. <head>
  71. <title>info</title>
  72. <Meta charset="UTF-8">
  73. </head>
  74. <body>
  75. {{.}}
  76. </body>
  77. </html>
  78. `
  79. tmpl,err := template.New("提示信息").Parse(base)
  80. if err != nil {
  81. panic(err)
  82. }
  83.  
  84. err = tmpl.Execute(w,template.HTML(info))
  85. if err != nil {
  86. panic(err)
  87. }
  88. }
  89.  
  90. const FILENAME = "Guestbook.log"
  91.  
  92. func readInfo() (string,error) {
  93. body,err := IoUtil.ReadFile(FILENAME)
  94. if err != nil {
  95. return "",err
  96. }
  97. return string(body),nil
  98. }
  99.  
  100. func writeInfo(str string) bool {
  101. f,err := os.OpenFile(FILENAME,os.O_RDWR|os.O_APPEND|os.O_CREATE,os.ModeType)
  102. if err != nil {
  103. panic(err)
  104. }
  105. defer f.Close()
  106. _,err = f.WriteString(str)
  107. if err != nil {
  108. panic(err)
  109. }
  110. return true
  111. }



其guestbook.tpl:

  1. {{define "guestbook"}}
  2. <html>
  3. <head>
  4. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>留言本</title>
  6. <style type="text/css">
  7. body{
  8. margin:0;
  9. padding:0;
  10. background:#EAEAEA;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15.  
  16. {{template "header"}}
  17.  
  18. {{.List}}
  19.  
  20. <center><a href="http://127.0.0.1:8055/add">增加留言</a></center>
  21.  
  22. {{template "footer"}}
  23.  
  24. </body>
  25. </html>
  26. {{end}}

实现的效果如下:





整个源码打包在此: 点击下载


MAIL: xcl_168@aliyun.com

BLOG: http://blog.csdn.net/xcl168

猜你在找的Go相关文章