SlideShare a Scribd company logo
1 of 16
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

Why you should care about Go (Golang)
Why you should care about Go (Golang)Why you should care about Go (Golang)
Why you should care about Go (Golang)Aaron Schlesinger
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewMarkus Schneider
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golangBasil N G
 
Golang (Go Programming Language)
Golang (Go Programming Language)Golang (Go Programming Language)
Golang (Go Programming Language)ShubhamMishra485
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageAniruddha Chakrabarti
 
Golang getting started
Golang getting startedGolang getting started
Golang getting startedHarshad Patil
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go langAmal Mohan N
 
[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기OKKY
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
Introduction to Go language
Introduction to Go languageIntroduction to Go language
Introduction to Go languageTzar Umang
 
OpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-SideOpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-SideTim Burks
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programmingMahmoud Masih Tehrani
 

What's hot (20)

Why you should care about Go (Golang)
Why you should care about Go (Golang)Why you should care about Go (Golang)
Why you should care about Go (Golang)
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
GoLang Introduction
GoLang IntroductionGoLang Introduction
GoLang Introduction
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Golang (Go Programming Language)
Golang (Go Programming Language)Golang (Go Programming Language)
Golang (Go Programming Language)
 
Php technical presentation
Php technical presentationPhp technical presentation
Php technical presentation
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 
Golang getting started
Golang getting startedGolang getting started
Golang getting started
 
Go lang
Go langGo lang
Go lang
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
 
[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKYCON] 정진욱 - 테스트하기 쉬운 코드로 개발하기
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
Introduction to Go language
Introduction to Go languageIntroduction to Go language
Introduction to Go language
 
OpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-SideOpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-Side
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
Php
PhpPhp
Php
 
Concurrency With Go
Concurrency With GoConcurrency With Go
Concurrency With Go
 

Viewers also liked

JSONSchema with golang
JSONSchema with golangJSONSchema with golang
JSONSchema with golangSuraj 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 evolutionYoni 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 MizunoNexus FrontierTech
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golangYoni Davidson
 
Golang start and tips
Golang start and tipsGolang start and tips
Golang start and tipsAaron King
 
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 1Robert Stern
 
A microservice architecture based on golang
A microservice architecture based on golangA microservice architecture based on golang
A microservice architecture based on golangGianfranco Reppucci
 

Viewers also liked (12)

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
 
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 PHPStephan Schmidt
 
HTML (Basic to Advance)
HTML (Basic to Advance)HTML (Basic to Advance)
HTML (Basic to Advance)Coder Tech
 
Django Translations: Internationalization
Django Translations: Internationalization Django Translations: Internationalization
Django Translations: Internationalization Jaya Silwal
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
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-Webfanqstefan
 
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 WorkshopHolden 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 W09Bastian 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

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

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.