From 73d5d8c41d370c72925209ebd7e00194b8e6c17d Mon Sep 17 00:00:00 2001 From: Dennis Durling Date: Fri, 12 Jan 2018 10:27:20 -0800 Subject: [PATCH 1/4] adding documentation --- .../05_performant-parsing_func-init/main.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/004_parse_execute/05_performant-parsing_func-init/main.go b/004_parse_execute/05_performant-parsing_func-init/main.go index 4fdfaa40..218cf837 100644 --- a/004_parse_execute/05_performant-parsing_func-init/main.go +++ b/004_parse_execute/05_performant-parsing_func-init/main.go @@ -6,9 +6,20 @@ import ( "text/template" ) +// creates a pointer to the template object, basically creating an empty object that +// has all the methods but no data yet. var tpl *template.Template +// func init() seems to be a standard function that gets auto run just like main +// but probably before main func init() { + // + // populate the pointer object using ParseGlob + // function call (Must) taking as an argument the results of a function call (ParseGlob) + // template.Must takes in a template pointer and an error as it's input + // which is exactly what most of the parsing methods return + // this would be a good place (the Must method) to do auto error codes and whatnot + // tpl = template.Must(template.ParseGlob("templates/*")) } From 2eeadbeb8e4556a2986ee846b093484d7ed99fd2 Mon Sep 17 00:00:00 2001 From: Dennis Durling Date: Fri, 12 Jan 2018 10:29:42 -0800 Subject: [PATCH 2/4] done with 05_performant-parsing_func-init. --- 004_parse_execute/05_performant-parsing_func-init/main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/004_parse_execute/05_performant-parsing_func-init/main.go b/004_parse_execute/05_performant-parsing_func-init/main.go index 218cf837..180a633b 100644 --- a/004_parse_execute/05_performant-parsing_func-init/main.go +++ b/004_parse_execute/05_performant-parsing_func-init/main.go @@ -20,25 +20,31 @@ func init() { // which is exactly what most of the parsing methods return // this would be a good place (the Must method) to do auto error codes and whatnot // + // now any files in the subfolder templates will be in the tpl object tpl = template.Must(template.ParseGlob("templates/*")) } func main() { + // here we take the template object and call Execute which will execute + // the first template it runs into err := tpl.Execute(os.Stdout, nil) if err != nil { log.Fatalln(err) } + // ExecuteTemplate: explicit template execution err = tpl.ExecuteTemplate(os.Stdout, "vespa.gohtml", nil) if err != nil { log.Fatalln(err) } + // ExecuteTemplate: explicit template execution err = tpl.ExecuteTemplate(os.Stdout, "two.gohtml", nil) if err != nil { log.Fatalln(err) } + // ExecuteTemplate: explicit template execution err = tpl.ExecuteTemplate(os.Stdout, "one.gohtml", nil) if err != nil { log.Fatalln(err) From 5f71046cfb1b17e4b5ef252b27940ed74848daa3 Mon Sep 17 00:00:00 2001 From: Dennis Durling Date: Fri, 12 Jan 2018 10:31:39 -0800 Subject: [PATCH 3/4] moar --- 004_parse_execute/05_performant-parsing_func-init/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/004_parse_execute/05_performant-parsing_func-init/main.go b/004_parse_execute/05_performant-parsing_func-init/main.go index 180a633b..fabe4625 100644 --- a/004_parse_execute/05_performant-parsing_func-init/main.go +++ b/004_parse_execute/05_performant-parsing_func-init/main.go @@ -6,8 +6,8 @@ import ( "text/template" ) -// creates a pointer to the template object, basically creating an empty object that -// has all the methods but no data yet. +// creates a pointer to the Template "Type", basically just a container +// for Template type things and methods of course to manipulate them var tpl *template.Template // func init() seems to be a standard function that gets auto run just like main From 80588f5edd5d0b56a2f41c264ac7ef5d7d12302f Mon Sep 17 00:00:00 2001 From: Dennis Durling Date: Fri, 12 Jan 2018 10:36:04 -0800 Subject: [PATCH 4/4] that was quick --- 005_data/01/main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/005_data/01/main.go b/005_data/01/main.go index eb1a34fb..7f897173 100644 --- a/005_data/01/main.go +++ b/005_data/01/main.go @@ -6,13 +6,19 @@ import ( "text/template" ) +// creates a pointer (container for) to a Template "Type" var tpl *template.Template func init() { + // init function gets run first + // populate the tpl pointer with a Template object + // pointing to a specific template (tpl.gohtml in this case) tpl = template.Must(template.ParseFiles("tpl.gohtml")) } func main() { + // ExecuteTemplate takes a writer (Stdout here) a template name + // and possibly values or nil and executes it err := tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", 42) if err != nil { log.Fatalln(err)