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
260
Money, Sex and Evolution (v3)
sausheong
0
110
Polyglot
sausheong
0
94
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
202512_AIoT.pdf
iotcomjpadmin
0
150
Entity Framework Core におけるIN句クエリ最適化について
htkym
0
130
2025年の医用画像AI/AI×medical_imaging_in_2025_generated_by_AI
tdys13
0
140
"人"が頑張るAI駆動開発
yokomachi
1
630
[Data & AI Summit '25 Fall] AIでデータ活用を進化させる!Google Cloudで作るデータ活用の未来
kirimaru
0
4k
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
6
1.8k
モダンデータスタックの理想と現実の間で~1.3億人Vポイントデータ基盤の現在地とこれから~
taromatsui_cccmkhd
2
280
意外と知らない状態遷移テストの世界
nihonbuson
PRO
1
290
Claude Skillsの テスト業務での活用事例
moritamasami
1
110
フィッシュボウルのやり方 / How to do a fishbowl
pauli
2
400
半年で、AIゼロ知識から AI中心開発組織の変革担当に至るまで
rfdnxbro
0
150
Authlete で実装する MCP OAuth 認可サーバー #CIMD の実装を添えて
watahani
0
210
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
0
67
How to Talk to Developers About Accessibility
jct
1
87
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
190
sira's awesome portfolio website redesign presentation
elsirapls
0
91
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
260
Scaling GitHub
holman
464
140k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
98
Unsuck your backbone
ammeep
671
58k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.5k
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