Skip to content

Instantly share code, notes, and snippets.

@krishbhanushali
Created March 29, 2021 19:49
Show Gist options
  • Save krishbhanushali/c42c21a738a88ed726b9eca9dc5559b8 to your computer and use it in GitHub Desktop.
Save krishbhanushali/c42c21a738a88ed726b9eca9dc5559b8 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"os"
"text/template"
)
type Employee struct {
FirstName string
LastName string
EmailAddress string
Manager bool
}
func (e *Employee) IsManager() bool {
return e.Manager
}
func main() {
employees := []Employee{
{
FirstName: "John",
LastName: "Doe",
EmailAddress: "john.doe@mailer.com",
Manager: false,
},
{
FirstName: "Chloe",
LastName: "Dowd",
EmailAddress: "chloe.dowd@mailer.com",
Manager: true,
},
}
const sample = `{{range .}}
Name: {{.FirstName}} {{.LastName}}
Email Address: {{.EmailAddress}}
Manager: {{if .IsManager}} Yes {{else}} No {{end}}
{{end}}`
t := template.Must(template.New("sampleTest").Parse(sample))
err := t.Execute(os.Stdout, employees)
if err != nil {
log.Println(err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment