一. 项目结构

  • 在Go语言中web项目标准结构如下

    1. --项目名
    2. --src
    3. --static
    4. --css
    5. --images
    6. --js
    7. --view
    8. --index.html
    9. --main.go
  • Go语言标准库中html/template包提供了html模版支持,把HTML当作模版可以在访问控制器时显示HTML模版信息

    • 这也符合标准的MVC思想

      二.HTML模版显示

  • 使用template.ParseFiles()可以解析多个模版文件

    1. // ParseFiles creates a new Template and parses the template definitions from
    2. // the named files. The returned template's name will have the (base) name and
    3. // (parsed) contents of the first file. There must be at least one file.
    4. // If an error occurs, parsing stops and the returned *Template is nil.
    5. //
    6. // When parsing multiple files with the same name in different directories,
    7. // the last one mentioned will be the one that results.
    8. // For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template
    9. // named "foo", while "a/foo" is unavailable.
    10. func ParseFiles(filenames ...string) (*Template, error) {
    11. return parseFiles(nil, filenames...)
    12. }
  • 把模版信息响应写入到输出流中

    1. // Execute applies a parsed template to the specified data object,
    2. // writing the output to wr.
    3. // If an error occurs executing the template or writing its output,
    4. // execution stops, but partial results may already have been written to
    5. // the output writer.
    6. // A template may be executed safely in parallel, although if parallel
    7. // executions share a Writer the output may be interleaved.
    8. func (t *Template) Execute(wr io.Writer, data interface{}) error {
    9. if err := t.escape(); err != nil {
    10. return err
    11. }
    12. return t.text.Execute(wr, data)
    13. }
  • 代码演示,显示index.html信息

    • 因为配置的pattern为”/“所以资源路径任意,都可以访问到这个HTML ```go package main

import ( “net/http” “html/template” )

func welcome(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles(“view/index.html”) t.Execute(w, nil) //第二个参数表示向模版传递的数据 }

func main() { server := http.Server{Addr: “:8090”} http.HandleFunc(“/“, welcome) server.ListenAndServe() }

  1. <a name="e42627df"></a>
  2. # 三.引用静态文件
  3. - 把静态文件放入到特定的文件夹中,使用Go语言的文件服务就可以进行加载
  4. - 项目结构

—项目 —static —js —index.js —view —index.html —main.go

  1. - index.html代码如下
  2. ```html
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <title>Title</title>
  8. <!--路径以斜杠开头,表示项目根目录下-->
  9. <script type="text/javascript" src="/static/js/index.js"></script>
  10. </head>
  11. <body>
  12. 这是要显示的html页面信息<br/>
  13. <button onclick="myclick()">按钮</button>
  14. </body>
  15. </html>
  • index.js代码如下

    1. function myclick(){
    2. alert("您点击了按钮")
    3. }
  • 代码示例 ```go package main

import ( “net/http” “html/template” )

func welcome(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles(“view/index.html”) t.Execute(w, nil) //第二个参数表示向模版传递的数据 }

func main() { server := http.Server{Addr: “:8090”} / 访问url以/static/开头,就会把访问信息映射到指定的目录中 / http.Handle(“/static/“, http.StripPrefix(“/static/“, http.FileServer(http.Dir(“static”)))) http.HandleFunc(“/“, welcome) server.ListenAndServe() } ```