Html/template .Parse is not defined


I am brand new to Go, started learning it less then a manth ago for a pet project. Coming from C/C++ I quite like it a lot, thanks!!!

I am building my pet project using code from the examples and I fear I have found a bug or an inconsistency in documentation.

I am trying to use template.Parse(string) but it is NOT defined when I build my code. If I replace it with template.ParseFiles(filename) then it works great.

Just to double check that I do not have a wrong version of Go installed (I am under Ubuntu 17.10 and followed the online instructions to install Go), I have tried the very same on the interactive “Try Go” home page and I have the very same problem: template.Parse is not defined.

// You can edit this code!
// Click here and start typing.
package main

import "fmt"
import “html/template”

func main() {
t,_ := template.Parse(“just test that it builds”)
fmt.Println(“Hello, 世界”)

The weird thing is that it is properly described in the Go packages and specifically at:

Any suggestion?

thank you

A Gallo

1 Like

Your code probably should look like this:

t := template.New("T")
t.Parse("just test that it builds")
1 Like

Note that the signature of this func is

func (t *Template) Parse(text string) (*Template, error)

which makes it a method of a Template instance. To be able to call Parse, you need to build such an instance first, as @geosoft1 suggested.


Thanks, it works. What still escapes me is that ParseFiles has the same signature:

func (t *Template) ParseFiles(filenames …string) (*Template, error)
func (t *Template) Parse(text string) (*Template, error)

So why with ParseFiles it does BUILD while with Parse it DOES NOT BUILD? If it did build with both or did not build with either, I would have proceeded further in debugging and double checking the reference code I have been studying and adapting from, which is:

Just go down to the usage of templates and ParseFiles, I started dynamically modifying the template from the Golang code before parsing it as I needed to add more code with a for loop rather than just reading from the file.

Thanks a lot, you have helped me move forward anyway!

There is also a package level ParseFiles() function which is what you’re calling when you say template.ParseFiles(...).


There is also a package level ParseFiles() function which is what you’re calling when you say

Thank you so much, this is the exact answer that explains the different behaviour I faced with Parse and ParseFiles.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.