Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "net/http"
  5. "text/template"
  6. )
  7.  
  8. func main() {
  9. http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
  10. w.Header().Add("Content Type", "text/html")
  11. templates := template.New("template")
  12. templates.New("test").Parse(doc)
  13. templates.New("header").Parse(header)
  14. templates.New("footer").Parse(footer)
  15. context := Context{
  16. [3]string{"Lemon", "Orange", "Apple"},
  17. "the title",
  18. }
  19. templates.Lookup("test").Execute(w, context)
  20.  
  21. })
  22.  
  23. http.ListenAndServe(":80", nil)
  24. }
  25.  
  26. const doc = `
  27. {{template "header" . Title}}
  28. <body>
  29. <h1>List of Fruit</h1>
  30. <ul>
  31. {{range .Fruit}}
  32. <li>{{.}}</li>
  33. {{end}}
  34. </ul>
  35. </body>
  36. `
  37.  
  38. const header = `
  39. <!DOCTYPE html>
  40. <html>
  41. <head><title>{{.}}</title></head>
  42. `
  43.  
  44. const footer = `
  45. </html>
  46. `
  47.  
  48. type Context struct {
  49. Fruit [3]string
  50. Title string
  51. }
  52.  
  53. 2015/07/01 13:53:14 http: panic serving 127.0.0.1:56994: runtime error: invalid memory address or nil pointer dereference
  54. goroutine 5 [running]:
  55. net/http.func·011()
  56. c:/go/src/net/http/server.go:1130 +0xc2
  57. text/template.errRecover(0xc082097b48)
  58. c:/go/src/text/template/exec.go:100 +0xc3
  59. text/template.(*Template).Execute(0x0, 0x353378, 0xc082070280, 0x6a5000, 0xc082008840, 0x0, 0x0)
  60. c:/go/src/text/template/exec.go:137 +0x249
  61. main.func·001(0x353210, 0xc082070280, 0xc082020000)
  62. D:/Go/src/main/main.go:19 +0x325
  63. net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc082070280, 0xc082020000)
  64. c:/go/src/net/http/server.go:1265 +0x48
  65. net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc082070280, 0xc082020000)
  66. c:/go/src/net/http/server.go:1541 +0x184
  67. net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc082070280, 0xc082020000)
  68. c:/go/src/net/http/server.go:1703 +0x1a1
  69. net/http.(*conn).serve(0xc082070000)
  70. c:/go/src/net/http/server.go:1204 +0xb5e
  71. created by net/http.(*Server).Serve
  72. c:/go/src/net/http/server.go:1751 +0x365
  73. 2015/07/01 13:53:14 http: panic serving 127.0.0.1:56995: runtime error: invalid memory address or nil pointer dereference
  74. goroutine 6 [running]:
  75. net/http.func·011()
  76. c:/go/src/net/http/server.go:1130 +0xc2
  77. text/template.errRecover(0xc082067b48)
  78. c:/go/src/text/template/exec.go:100 +0xc3
  79. text/template.(*Template).Execute(0x0, 0x353378, 0xc082070320, 0x6a5000, 0xc082008a40, 0x0, 0x0)
  80. c:/go/src/text/template/exec.go:137 +0x249
  81. main.func·001(0x353210, 0xc082070320, 0xc0820200d0)
  82. D:/Go/src/main/main.go:19 +0x325
  83. net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc082070320, 0xc0820200d0)
  84. c:/go/src/net/http/server.go:1265 +0x48
  85. net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc082070320, 0xc0820200d0)
  86. c:/go/src/net/http/server.go:1541 +0x184
  87. net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc082070320, 0xc0820200d0)
  88. c:/go/src/net/http/server.go:1703 +0x1a1
  89. net/http.(*conn).serve(0xc0820701e0)
  90. c:/go/src/net/http/server.go:1204 +0xb5e
  91. created by net/http.(*Server).Serve
  92. c:/go/src/net/http/server.go:1751 +0x365
  93. 2015/07/01 13:53:14 http: panic serving 127.0.0.1:56997: runtime error: invalid memory address or nil pointer dereference
  94. goroutine 13 [running]:
  95. net/http.func·011()
  96. c:/go/src/net/http/server.go:1130 +0xc2
  97. text/template.errRecover(0xc08201fb48)
  98. c:/go/src/text/template/exec.go:100 +0xc3
  99. text/template.(*Template).Execute(0x0, 0x353378, 0xc082070460, 0x6a5000, 0xc082008dc0, 0x0, 0x0)
  100. c:/go/src/text/template/exec.go:137 +0x249
  101. main.func·001(0x353210, 0xc082070460, 0xc0820209c0)
  102. D:/Go/src/main/main.go:19 +0x325
  103. net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc082070460, 0xc0820209c0)
  104. c:/go/src/net/http/server.go:1265 +0x48
  105. net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc082070460, 0xc0820209c0)
  106. c:/go/src/net/http/server.go:1541 +0x184
  107. net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc082070460, 0xc0820209c0)
  108. c:/go/src/net/http/server.go:1703 +0x1a1
  109. net/http.(*conn).serve(0xc0820703c0)
  110. c:/go/src/net/http/server.go:1204 +0xb5e
  111. created by net/http.(*Server).Serve
  112. c:/go/src/net/http/server.go:1751 +0x365
  113. 2015/07/01 13:53:15 http: panic serving 127.0.0.1:56998: runtime error: invalid memory address or nil pointer dereference
  114. goroutine 17 [running]:
  115. net/http.func·011()
  116. c:/go/src/net/http/server.go:1130 +0xc2
  117. text/template.errRecover(0xc082067b48)
  118. c:/go/src/text/template/exec.go:100 +0xc3
  119. text/template.(*Template).Execute(0x0, 0x353378, 0xc0820701e0, 0x6a5000, 0xc082008840, 0x0, 0x0)
  120. c:/go/src/text/template/exec.go:137 +0x249
  121. main.func·001(0x353210, 0xc0820701e0, 0xc0820200d0)
  122. D:/Go/src/main/main.go:19 +0x325
  123. net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc0820701e0, 0xc0820200d0)
  124. c:/go/src/net/http/server.go:1265 +0x48
  125. net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc0820701e0, 0xc0820200d0)
  126. c:/go/src/net/http/server.go:1541 +0x184
  127. net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc0820701e0, 0xc0820200d0)
  128. c:/go/src/net/http/server.go:1703 +0x1a1
  129. net/http.(*conn).serve(0xc082070000)
  130. c:/go/src/net/http/server.go:1204 +0xb5e
  131. created by net/http.(*Server).Serve
  132. c:/go/src/net/http/server.go:1751 +0x365
  133.  
  134. template: test:2: function "Title" not defined
  135.  
  136. {{template "header" . Title}}
  137.  
  138. {{template "header" .Title}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement