Note that there are some explanatory texts on larger screens.

plurals
  1. POtext/template issue Parse() vs. ParseFiles()
    primarykey
    data
    text
    <p>I'm trying to do some simple work with the text/template package. The sample given at the top of <a href="http://golang.org/pkg/text/template/" rel="nofollow">template</a> is what I'm working with.</p> <p>How do I write the 'parsed' file so <code>template.ParseFiles()</code> properly reads and executes it?</p> <pre><code>package main import ( "text/template" "os" ) type Inventory struct { Material string Count uint } func main() { sweaters := Inventory{"wool", 17} tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}}") // tmpl, err := template.New("test").ParseFiles("file.txt") if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, sweaters) if err != nil { panic(err) } } /* Contents of file.txt: {{.Count}} items are made of {{.Material}} Error thrown: panic: template: test:1: "test" is an incomplete or empty template goroutine 1 [running]: main.main() /tmp/templates/t.go:19 +0x21a goroutine 2 [syscall]: created by runtime.main /var/tmp/portage/dev-lang/go-1.0.1/work/go/src/pkg/runtime/proc.c:221 */ </code></pre> <p>I have a copy of this code posted at the golang playground <a href="http://play.golang.org/p/C3oTaFFRmp" rel="nofollow">here</a></p> <p>Edit #1: I've been doing some research on this issue... since it's the <code>Execute()</code> method that actually throws the exception, and not the <code>ParseFiles()</code> part, I checked the method definition:</p> <pre><code>// Execute applies a parsed template to the specified data object, // and writes the output to wr. func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { defer errRecover(&amp;err) value := reflect.ValueOf(data) state := &amp;state{ tmpl: t, wr: wr, line: 1, vars: []variable{{"$", value}}, } if t.Tree == nil || t.Root == nil { state.errorf("%q is an incomplete or empty template", t.name) } state.walk(value, t.Root) return } </code></pre> <p>So, on a hunch, I dumped the value of t.Tree for the inline 'non-file' style, tmpl is: <code>&amp;parse.Tree{Name:"test", Root:(*parse.ListNode)(0xf840030700), funcs:[]map[string]interface {}(nil), lex:(*parse.lexer)(nil), token:[2]parse.item{parse.item{typ:6, val:""}, parse.item{typ:9, val:"{{"}}, peekCount:1, vars:[]string(nil)}</code> and when ran with <code>ParseFiles()</code>, tmpl is: <code>(*parse.Tree)(nil)</code>. I find it odd that one is a dereference, and one value is a pointer. This may help solve the riddle</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload