Home > Backend Development > Golang > How to use template engine in Go language

How to use template engine in Go language

藏色散人
Release: 2020-09-12 13:30:06
forward
4456 people have browsed it

#golang tutorial

How to use template engine in Go language

Use template engine, hope it will be helpful to friends in need!

How to use template engine in Go language

1 Overview

When processing the response body, the most common way is to send the processed HTML code. Since the data needs to be Embedded into HTML, then a template engine is the best choice.

In the Go language, the html/template

package is provided to implement the related functions of the template engine. Quick usage example:

main.go

package mainimport (
  "html/template"
  "log"
  "net/http")func main() {
  // 设置 处理函数
  http.HandleFunc("/", TestAction)
   开启监听(监听浏览器请求)
  log.Fatal(http.ListenAndServe(":8084", nil))}func TestAction(w http.ResponseWriter, r *http.Request) {
  // 解析模板
  t, _ := template.ParseFiles("template/index.html")
  // 设置模板数据
  data := map[string]interface{}{
    "User": "小韩说课",
    "List": []string{"Go", "Python", "PHP", "JavaScript"},
  }
  // 渲染模板,发送响应
  t.Execute(w, data)}
Copy after login

template/index.html

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>小韩说课</title></head><body>Hello, {{ .User }}<br>你熟悉的技术:<ul>{{ range .List }}    <li>{{.}}</li>{{end}}</ul></body></html>
Copy after login
Execution result:

The above code completes the basic use of the template engine, including parsing templates, rendering data, and responding to result operations. Details follow.

2 Parse template

Functiontemplate.ParseFiles(filenames ...string) (*Template, error)### You can parse template files and get template objects . The parameter is the template file. At the same time, the file name of the template file (excluding the suffix) will be used as the name of the template. ######You can also use ###template.New("name").Parse(src string)### to create a template object and complete the parsing of the template content. ######3 Apply data and send response######Function###func (t *Template) Execute(wr io.Writer, data interface{}) error### Apply data to parsed on the template and write the output to wr. If an error occurs during execution, execution will stop, but some data of wr may have been written. ######data can accept any type. The most common type is: ###map[string]interface{}###. Different subscripts are used to distinguish part of the allocated data. Use ###.User###, ###.List### in the template to access User and List in the distribution data. ######over! ######

The above is the detailed content of How to use template engine in Go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template