Created
March 29, 2021 19:49
-
-
Save krishbhanushali/c42c21a738a88ed726b9eca9dc5559b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]", | |
Manager: false, | |
}, | |
{ | |
FirstName: "Chloe", | |
LastName: "Dowd", | |
EmailAddress: "[email protected]", | |
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