SlideShare a Scribd company logo
GOLANG TEMPLATEGOLANG TEMPLATE
By,
Karthick Kumar V
Golang an OverviewGolang an Overview
● Go is a compiled language
● Its syntax loosely derived from C
● It is a fast-compiled, garbage-collected, concurrent
systems programming language
● It can compile a large project within a few seconds
● It is designed for multi-core computers
Template an OverviewTemplate an Overview
● Templates are a way to merge generic text with more
specific text
● Example : ERB / Haml – Template Engine for Ruby
Templating in GolangTemplating in Golang
● In Go Template Package is to handle Templating
● To rendering the output
● It has 2 types of Template Package
1. Text Template
- import “text/template”
2. HTML Template
- import “html/template”
Text Template PackageText Template Package
● Package Text template implements data-driven templates
for generating textual output
● It will not support generating HTML outputs
● Execution of the template is represented by a period '.' and
called "dot"
HTML Template PackageHTML Template Package
● Package HTML template implements data-driven
templates for generating HTML output safe against code
injection.
● It provides the same interface as package text/template
● This package wraps package text/template so you can
share its template API to parse and execute HTML
templates safely
Basic Syntax of TemplatingBasic Syntax of Templating
● Field substitution - {{.FieldName}}
- To include the content of a field within a template
- It is enclose within curly braces and add a dot at the
beginning
- Example : <p>Hello, {{.Text}}</P>
- It is similar to writing ERB in Ruby
- Example : <p>Hello, <%= @name %></p>
Basic Syntax of TemplatingBasic Syntax of Templating
● Nested fields - {{range}} and {{with}}
- Printing the output in Loop
- {{range}} just like range in Go
- {{with}} lets you write the same object name once and
use {{.}} as shorthand
● Pipeline - {{pipeline}}
- The default textual representation of the value of the
pipeline is copied to the output. Ex: {{. | html}}
Basic Syntax of TemplatingBasic Syntax of Templating
● Conditions – {{if}},{{else if}} and {{else}}
- To check for conditions in templates
- We can use the if-else syntax just like you do in regular
Go programs
- We cannot use conditional expressions in if, for
instance
- Example : {{.Mail}}=="atomic@gmail.com"
- Only boolean values are acceptable
Basic Syntax of TemplatingBasic Syntax of Templating
● Template variables - {{$variable}}
- To use local variables in templates
- We can use them with the with, range and if keywords
- their scope is between these keywords and {{end}}
- Example :
{{with $x := "output" | printf "%q"}}{{$x}}{{end}}
Basic Syntax of TemplatingBasic Syntax of Templating
● Nested templates - “Code Reusability”
- The templates can be reused across other templates
- like headers and footers of a blog
- We can declare header, content and footer as sub-
templates
- Example :
{{define "sub-template"}}content{{end}}
Basic Syntax of TemplatingBasic Syntax of Templating
● Must - “function”
- The template package has a function called Must
- It is used or validating templates, like the matching of
braces, comments, and variables
- Example :
template.Must(template.ParseFiles("index_temp.html")
Methods in TemplatingMethods in Templating
● To load the Template from a string or file and then perform
the merge
1. Parse
2. ParseFile
3. Execute
4. ExecuteTemplate
5. os.Stdout
Methods DescriptionMethods Description
● Parse
- It is used to render textual data
- Its is used in text/template
● ParseFile
- It is used to render HTML files
- It is used in html/template
Methods DescriptionMethods Description
● Execute
- It is used to display textual output
- Its is used in text/template
● ExecuteTemplate
- It is used to load HTML output
- It is used in html/template
● os.Stdout
- It is a standard output to print out the merged data
SummarySummary
● To combine dynamic data with templates using techniques
including printing data in loops, template functions and
nested templates
● The input text for a template is UTF-8-encoded text in any
format. "Actions" data evaluations or control structures are
delimited by "{{" and "}}"; all text outside actions is
copied to the output unchanged.

More Related Content

What's hot

Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
Markus Schneider
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
Ishin Vin
 
Go Language presentation
Go Language presentationGo Language presentation
Go Language presentation
Gh-Mohammed Eldadah
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
jgrahamc
 
An introduction to programming in Go
An introduction to programming in GoAn introduction to programming in Go
An introduction to programming in Go
David Robert Camargo de Campos
 
Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
Wei-Ning Huang
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
Mahmoud Masih Tehrani
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
Slawomir Dorzak
 
Git Tutorial I
Git Tutorial IGit Tutorial I
Git Tutorial I
Jim Yeh
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practice
Guilherme Garnier
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
Victor S. Recio
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
Designveloper
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
Roman Elizarov
 
Clean code
Clean code Clean code
Clean code
Achintya Kumar
 
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel FernandesKernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Anne Nicolas
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
Theo Jungeblut
 
Workshop on Git and GitHub
Workshop on Git and GitHubWorkshop on Git and GitHub
Workshop on Git and GitHub
DSCVSSUT
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Luis Goldster
 
Dev112 let's calendar that
Dev112   let's calendar thatDev112   let's calendar that
Dev112 let's calendar that
Howard Greenberg
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
Oliver N
 

What's hot (20)

Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Go Language presentation
Go Language presentationGo Language presentation
Go Language presentation
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
An introduction to programming in Go
An introduction to programming in GoAn introduction to programming in Go
An introduction to programming in Go
 
Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
 
Git Tutorial I
Git Tutorial IGit Tutorial I
Git Tutorial I
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practice
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 
Clean code
Clean code Clean code
Clean code
 
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel FernandesKernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
 
Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
 
Workshop on Git and GitHub
Workshop on Git and GitHubWorkshop on Git and GitHub
Workshop on Git and GitHub
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Dev112 let's calendar that
Dev112   let's calendar thatDev112   let's calendar that
Dev112 let's calendar that
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
 

Viewers also liked

Go 1.8 Release Party
Go 1.8 Release PartyGo 1.8 Release Party
Go 1.8 Release Party
Rodolfo Carvalho
 
JSONSchema with golang
JSONSchema with golangJSONSchema with golang
JSONSchema with golang
Suraj Deshmukh
 
The internet of things in now , see how golang is a part of this evolution
The internet of things in now , see how golang is a part of this evolutionThe internet of things in now , see how golang is a part of this evolution
The internet of things in now , see how golang is a part of this evolution
Yoni Davidson
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern
理 傅
 
Come With Golang
Come With GolangCome With Golang
Come With Golang
尚文 曾
 
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
 [INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno [INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
Nexus FrontierTech
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golang
Yoni Davidson
 
Golang start and tips
Golang start and tipsGolang start and tips
Golang start and tips
Aaron King
 
Golang for OO Programmers
Golang for OO ProgrammersGolang for OO Programmers
Golang for OO Programmers
khalid Nowaf Almutiri
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
Robert Stern
 
A microservice architecture based on golang
A microservice architecture based on golangA microservice architecture based on golang
A microservice architecture based on golang
Gianfranco Reppucci
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
Bo-Yi Wu
 
Functional go
Functional goFunctional go
Functional go
Geison Goes
 

Viewers also liked (13)

Go 1.8 Release Party
Go 1.8 Release PartyGo 1.8 Release Party
Go 1.8 Release Party
 
JSONSchema with golang
JSONSchema with golangJSONSchema with golang
JSONSchema with golang
 
The internet of things in now , see how golang is a part of this evolution
The internet of things in now , see how golang is a part of this evolutionThe internet of things in now , see how golang is a part of this evolution
The internet of things in now , see how golang is a part of this evolution
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern
 
Come With Golang
Come With GolangCome With Golang
Come With Golang
 
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
 [INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno [INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golang
 
Golang start and tips
Golang start and tipsGolang start and tips
Golang start and tips
 
Golang for OO Programmers
Golang for OO ProgrammersGolang for OO Programmers
Golang for OO Programmers
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
A microservice architecture based on golang
A microservice architecture based on golangA microservice architecture based on golang
A microservice architecture based on golang
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
Functional go
Functional goFunctional go
Functional go
 

Similar to Golang Template

XML Transformations With PHP
XML Transformations With PHPXML Transformations With PHP
XML Transformations With PHP
Stephan Schmidt
 
HTML (Basic to Advance)
HTML (Basic to Advance)HTML (Basic to Advance)
HTML (Basic to Advance)
Coder Tech
 
Advance HTML
Advance HTMLAdvance HTML
Advance HTML
VijaySingh790398
 
Django Translations: Internationalization
Django Translations: Internationalization Django Translations: Internationalization
Django Translations: Internationalization
Jaya Silwal
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
BG Java EE Course
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
Jussi Pohjolainen
 
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handlingSuite Solutions
 
Developing web apps using Erlang-Web
Developing web apps using Erlang-WebDeveloping web apps using Erlang-Web
Developing web apps using Erlang-Web
fanqstefan
 
Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...webhostingguy
 
Introduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines WorkshopIntroduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines Workshop
Holden Karau
 
The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09
Bastian Feder
 

Similar to Golang Template (20)

lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
 
lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
 
Routing
RoutingRouting
Routing
 
XML Transformations With PHP
XML Transformations With PHPXML Transformations With PHP
XML Transformations With PHP
 
HTML (Basic to Advance)
HTML (Basic to Advance)HTML (Basic to Advance)
HTML (Basic to Advance)
 
Advance HTML
Advance HTMLAdvance HTML
Advance HTML
 
topic_perlcgi
topic_perlcgitopic_perlcgi
topic_perlcgi
 
topic_perlcgi
topic_perlcgitopic_perlcgi
topic_perlcgi
 
Web Designing
Web DesigningWeb Designing
Web Designing
 
Project Automation
Project AutomationProject Automation
Project Automation
 
Django Translations: Internationalization
Django Translations: Internationalization Django Translations: Internationalization
Django Translations: Internationalization
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
HTML/HTML5
HTML/HTML5HTML/HTML5
HTML/HTML5
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
 
Developing web apps using Erlang-Web
Developing web apps using Erlang-WebDeveloping web apps using Erlang-Web
Developing web apps using Erlang-Web
 
Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...
 
Introduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines WorkshopIntroduction to Spark ML Pipelines Workshop
Introduction to Spark ML Pipelines Workshop
 
The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09The Beauty And The Beast Php N W09
The Beauty And The Beast Php N W09
 

Recently uploaded

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 

Golang Template

  • 2. Golang an OverviewGolang an Overview ● Go is a compiled language ● Its syntax loosely derived from C ● It is a fast-compiled, garbage-collected, concurrent systems programming language ● It can compile a large project within a few seconds ● It is designed for multi-core computers
  • 3. Template an OverviewTemplate an Overview ● Templates are a way to merge generic text with more specific text ● Example : ERB / Haml – Template Engine for Ruby
  • 4. Templating in GolangTemplating in Golang ● In Go Template Package is to handle Templating ● To rendering the output ● It has 2 types of Template Package 1. Text Template - import “text/template” 2. HTML Template - import “html/template”
  • 5. Text Template PackageText Template Package ● Package Text template implements data-driven templates for generating textual output ● It will not support generating HTML outputs ● Execution of the template is represented by a period '.' and called "dot"
  • 6. HTML Template PackageHTML Template Package ● Package HTML template implements data-driven templates for generating HTML output safe against code injection. ● It provides the same interface as package text/template ● This package wraps package text/template so you can share its template API to parse and execute HTML templates safely
  • 7. Basic Syntax of TemplatingBasic Syntax of Templating ● Field substitution - {{.FieldName}} - To include the content of a field within a template - It is enclose within curly braces and add a dot at the beginning - Example : <p>Hello, {{.Text}}</P> - It is similar to writing ERB in Ruby - Example : <p>Hello, <%= @name %></p>
  • 8. Basic Syntax of TemplatingBasic Syntax of Templating ● Nested fields - {{range}} and {{with}} - Printing the output in Loop - {{range}} just like range in Go - {{with}} lets you write the same object name once and use {{.}} as shorthand ● Pipeline - {{pipeline}} - The default textual representation of the value of the pipeline is copied to the output. Ex: {{. | html}}
  • 9. Basic Syntax of TemplatingBasic Syntax of Templating ● Conditions – {{if}},{{else if}} and {{else}} - To check for conditions in templates - We can use the if-else syntax just like you do in regular Go programs - We cannot use conditional expressions in if, for instance - Example : {{.Mail}}=="atomic@gmail.com" - Only boolean values are acceptable
  • 10. Basic Syntax of TemplatingBasic Syntax of Templating ● Template variables - {{$variable}} - To use local variables in templates - We can use them with the with, range and if keywords - their scope is between these keywords and {{end}} - Example : {{with $x := "output" | printf "%q"}}{{$x}}{{end}}
  • 11. Basic Syntax of TemplatingBasic Syntax of Templating ● Nested templates - “Code Reusability” - The templates can be reused across other templates - like headers and footers of a blog - We can declare header, content and footer as sub- templates - Example : {{define "sub-template"}}content{{end}}
  • 12. Basic Syntax of TemplatingBasic Syntax of Templating ● Must - “function” - The template package has a function called Must - It is used or validating templates, like the matching of braces, comments, and variables - Example : template.Must(template.ParseFiles("index_temp.html")
  • 13. Methods in TemplatingMethods in Templating ● To load the Template from a string or file and then perform the merge 1. Parse 2. ParseFile 3. Execute 4. ExecuteTemplate 5. os.Stdout
  • 14. Methods DescriptionMethods Description ● Parse - It is used to render textual data - Its is used in text/template ● ParseFile - It is used to render HTML files - It is used in html/template
  • 15. Methods DescriptionMethods Description ● Execute - It is used to display textual output - Its is used in text/template ● ExecuteTemplate - It is used to load HTML output - It is used in html/template ● os.Stdout - It is a standard output to print out the merged data
  • 16. SummarySummary ● To combine dynamic data with templates using techniques including printing data in loops, template functions and nested templates ● The input text for a template is UTF-8-encoded text in any format. "Actions" data evaluations or control structures are delimited by "{{" and "}}"; all text outside actions is copied to the output unchanged.