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
310
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
120
Programming Complexity
sausheong
0
890
Rollicking Ruby Robots Rule the World
sausheong
0
240
Money, Sex and Evolution (v3)
sausheong
0
82
Polyglot
sausheong
0
63
Developing Web Applications with Go
sausheong
7
780
Money, Sex and Evolution
sausheong
1
84
A Tale of Two Frameworks
sausheong
0
72
Ruby, Rock and Roll
sausheong
3
290
Other Decks in Technology
See All in Technology
自分の軸足を見つけろ
tsuemura
2
600
アセスメントで紐解く、10Xのデータマネジメントの軌跡
10xinc
1
360
AIで進化するソフトウェアテスト:mablの最新生成AI機能でQAを加速!
mfunaki
0
120
【2025年度新卒技術研修】100分で学ぶ サイバーエージェントのデータベース 活用事例とMySQLパフォーマンス調査
cyberagentdevelopers
PRO
4
6.5k
OSSコントリビュートをphp-srcメンテナの立場から語る / OSS Contribute
sakitakamachi
0
1.3k
TopAppBar Composableをカスタムする
hunachi
0
170
AIエージェント開発における「攻めの品質改善」と「守りの品質保証」 / 2024.04.09 GPU UNITE 新年会 2025
smiyawaki0820
0
400
はじめてのSDET / My first challenge as a SDET
bun913
1
200
LangfuseでAIエージェントの 可観測性を高めよう!/Enhancing AI Agent Observability with Langfuse!
jnymyk
0
170
Amazon S3 Tables + Amazon Athena / Apache Iceberg
okaru
0
240
Spring Bootで実装とインフラをこれでもかと分離するための試み
shintanimoto
4
420
Amebaにおける Platform Engineeringの実践
kumorn5s
6
900
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Documentation Writing (for coders)
carmenintech
69
4.7k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.9k
Faster Mobile Websites
deanohume
306
31k
Building Flexible Design Systems
yeseniaperezcruz
329
38k
Practical Orchestrator
shlominoach
186
10k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The Pragmatic Product Professional
lauravandoore
33
6.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
19
1.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
Java REST API Framework Comparison - PWX 2021
mraible
30
8.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