Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
3 Things You May Not Know About The Go Template...
Search
Sau Sheong Chang
March 03, 2015
Technology
0
350
3 Things You May Not Know About The Go Template Engine
A short introduction to the Go template engine
Sau Sheong Chang
March 03, 2015
Tweet
Share
More Decks by Sau Sheong Chang
See All by Sau Sheong Chang
Genetic Algorithms with Go
sausheong
0
160
Programming Complexity
sausheong
0
1k
Rollicking Ruby Robots Rule the World
sausheong
0
270
Money, Sex and Evolution (v3)
sausheong
0
110
Polyglot
sausheong
0
96
Developing Web Applications with Go
sausheong
7
820
Money, Sex and Evolution
sausheong
1
110
A Tale of Two Frameworks
sausheong
0
82
Ruby, Rock and Roll
sausheong
3
300
Other Decks in Technology
See All in Technology
「駆動」って言葉、なんかカッコイイ_Mitz
comucal
PRO
0
130
2025-12-27 Claude CodeでPRレビュー対応を効率化する@機械学習社会実装勉強会第54回
nakamasato
4
1.4k
コールドスタンバイ構成でCDは可能か
hiramax
0
130
松尾研LLM講座2025 応用編Day3「軽量化」 講義資料
aratako
15
4.8k
ECS_EKS以外の選択肢_ROSA入門_.pdf
masakiokuda
1
120
Data Hubグループ 紹介資料
sansan33
PRO
0
2.5k
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
自己管理型チームと個人のセルフマネジメント 〜モチベーション編〜
kakehashi
PRO
4
700
BidiAgent と Nova 2 Sonic から考える音声 AI について
yama3133
2
140
Oracle Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
2
650
AI with TiDD
shiraji
1
330
Scrum Guide Expansion Pack が示す現代プロダクト開発への補完的視点
sonjin
0
230
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
58
41k
Side Projects
sachag
455
43k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
74
Everyday Curiosity
cassininazir
0
120
How to train your dragon (web standard)
notwaldorf
97
6.5k
How STYLIGHT went responsive
nonsquared
100
6k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
Ethics towards AI in product and experience design
skipperchong
1
150
Practical Orchestrator
shlominoach
190
11k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
80
Balancing Empowerment & Direction
lara
5
830
Transcript
3 Things You May Not Know About The Go
Template Engine Chang Sau Sheong Feb 2015
Obligatory stuff
What is a template engine?
None
None
Where’s Go template engine? Logic-‐less Go template
engine Embedded logic Mustache, Handlebars etc JSP, Haml, Jade etc
How does the Go template engine work?
text/template html/template
Parse template Execute template
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web
Programming</title> </head> <body> {{ . }} </body> </html>
package main import ( "net/http" "html/template" ) func process(w http.ResponseWriter,
r *http.Request) { t, _ := template.ParseFiles("tmpl.html") t.Execute(w, "Hello World!") } func main() { server := http.Server{ Addr: "127.0.0.1:8080", } http.HandleFunc("/process", process) server.ListenAndServe() }
The Go template engine is kinda cool
1. Pipelines
{{ p1 | p2 | p3 }}
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/ html; charset=utf-8"> <title>Go
Web Programming</title> </head> <body> {{ 12.3456 | printf "%.2f" }} </body> </html>
2. FuncTons
func formatDate(t time.Time) string { layout := "2006-01-02" return t.Format(layout)
} func process(w http.ResponseWriter, r *http.Request) { funcMap := template.FuncMap { "fdate": formatDate } t := template.New("tmpl.html").Funcs(funcMap) t, _ = t.ParseFiles("tmpl.html") t.Execute(w, time.Now()) }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>The date/time is {{ fdate . }}</div> </body> </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>The date/time is {{ . | fdate }}</div> </body> </html>
3. Context aware
func process(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("tmpl.html")
content := `I asked: <i>"What's up?"</i>` t.Execute(w, content) }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>{{ . }}</div> <div><a href="/{{ . }}">Path</a></div> <div><a href="/?q={{ . }}">Query</a></div> <div><a onclick="f('{{ . }}')">Onclick</a></div> </body> </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>I asked: <i>"What's up?"</ i></div> <div><a href="/I%20asked:%20%3ci%3e%22What%27s%20up? %22%3c/i%3e">Path</a></div> <div><a href="/?q=I%20asked%3a%20%3ci%3e%22What%27s %20up%3f%22%3c%2fi%3e">Query</a></div> <div><a onclick="f('I asked: \x3ci\x3e\x22What\x27s up?\x22\x3c\/i\x3e')">Onclick</a></div> </body> </html>
Context Content Original text I asked: <i>"What's up?"</i> {{ .
}} I asked: <i>"What's up?"</i> <a href="/{{ . }}"> I%20asked:%20%3ci%3e%22What%27s%20up?%22%3c/i%3e <a href="/?q={{ . }}"> I%20asked%3a%20%3ci%3e%22What%27s%20up%3f%22%3c %2fi%3e <a onclick="{{ . }}"> I asked: \x3ci\x3e\x22What\x27s up?\x22\x3c\/i\x3e
XSS
XSS Demo
hWp://manning.com/chang
Thank you
[email protected]
@sausheong