A bit of Go templates

A bit of Go Templates

This is a file called hello.gohtml:

<h1> Hello, {{.Name}} </h1>

While this is the go code:


package main

import (
	"html/template"
	"os"
)

type User struct {
	Name string
}

func main() {

	t, err := template.ParseFiles("hello.gohtml")
	if err != nil {
		panic(err)
	}

	user := User{
		Name: "Alex Rabocse",
	}

	t.Execute(os.Stdout, user)
	if err != nil {
		panic(err)
	}

}

Notes

  • If we “go run” the previous file, this is what we will get:
go run main.go
<h1> Hello, Alex Rabocse </h1>

 Share!

 
comments powered by Disqus