📌  相关文章
📜  golang 渲染 html 模板 - Go 编程语言 - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:45:00.662000             🧑  作者: Mango

代码示例2
// parser html via variable

package main

import (
    "net/http"
    "text/template"
)

type Context struct {
    Title  string
    Name   string
    Fruits [3]string
}

func main() {
    const doc = `


    
        {{.Title}}
    
    
        

Hi, {{.Name}}. The fruits are:

    {{range .Fruit}}
  • {{.}}
  • {{end}}
` http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { w.Header().Add("Content Type", "text/html") templates, _ := template.New("doc").Parse(doc) context := Context{ Title: "My Fruits", Name: "John", Fruits: [3]string{"Apple", "Lemon", "Orange"}, } templates.Lookup("doc").Execute(w, context) }) http.ListenAndServe(":8000", nil) }