Post

Go templates pipelines

Go templates pipelines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"os"
	"time"
	"fmt"
	"log"
	"math"
	"text/template"
)

var tpl *template.Template

func init() {
	fmt.Println("Initializing ...")
	// tpl = template.Must(template.ParseGlob("*.gohtml"))
	//  to incorporate some extra function into template object
	tpl = template.Must(template.New("").Funcs(fm).ParseGlob("pipelines.gohtml"))
}

//  define several functions to be used in templating
func monthDayYear(t time.Time) string{
	return t.Format("01-02-2006")
}




func double(x int) int {
	return x + x
}

func sqRoot(x float64) float64 {
	return math.Sqrt(x)
}

func square(x int) float64 {
	return math.Pow(float64(x), 2)
}

var fm = template.FuncMap{
	"fdateMDY": monthDayYear,
	"fdbl": double,
	"fsqrt": sqRoot,
	"fsq": square,
}


func main()  {

	// err := tpl.ExecuteTemplate(os.Stdout, "dateformatting.gohtml", time.Now())
	err := tpl.ExecuteTemplate(os.Stdout, "pipelines.gohtml", 3)

	if err != nil {
		log.Fatalln(err)
	}



}

This post is licensed under CC BY 4.0 by the author.