Using custom functions in html/template

319 views
Skip to first unread message

Alex Sinelnikov

unread,
Jun 14, 2015, 10:16:45 AM6/14/15
to golan...@googlegroups.com
I'm trying to add custom function using FuncMap:

var funcMap = template.FuncMap{
   
"sayHello": sayHello,
 
}

 func sayHello
() string {
 
return "Hello World"
 
}
tmpl
, err := template.New("base").Funcs(funcMap).ParseFiles(html_path)
tmpl
.Execute(writer, nil)


In html file i have
{{ sayHelo }}

This throws me an error
runtime error: invalid memory address or nil pointer dereference.



What am i doing wrong?

Tamás Gulácsi

unread,
Jun 14, 2015, 10:25:29 AM6/14/15
to golan...@googlegroups.com
Misspell.

Alex Sinelnikov

unread,
Jun 14, 2015, 10:28:11 AM6/14/15
to golan...@googlegroups.com
In my html code, there's a misspell, but thats just wrongly copied. In my code, everything spelled correctly

воскресенье, 14 июня 2015 г., 17:16:45 UTC+3 пользователь Alex Sinelnikov написал:

Alex Sinelnikov

unread,
Jun 14, 2015, 10:28:46 AM6/14/15
to golan...@googlegroups.com
Thanks, but that was a typo in code in this thread. 

воскресенье, 14 июня 2015 г., 17:25:29 UTC+3 пользователь Tamás Gulácsi написал:
Misspell.

Kiki Sugiaman

unread,
Jun 14, 2015, 11:56:17 AM6/14/15
to golan...@googlegroups.com
Once you do:

var funcMap = template.FuncMap{
   
"whatEver": sayHello,
}

Then you invoke in html using {{.whatEver}}, because now you invoke using the map's key, not its value.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiki Sugiaman

unread,
Jun 14, 2015, 2:15:33 PM6/14/15
to golan...@googlegroups.com
sigh, was drunk while giving answer.
I'll just leave a note here to invalidate my previous reply that everything should work without the typo.

Alex Sinelnikov

unread,
Jun 14, 2015, 3:22:51 PM6/14/15
to golan...@googlegroups.com
i guess there's something wrong with that map by itself. Ill check and return with more info


воскресенье, 14 июня 2015 г., 17:16:45 UTC+3 пользователь Alex Sinelnikov написал:
I'm trying to add custom function using FuncMap:

Alex Sinelnikov

unread,
Jun 14, 2015, 3:28:12 PM6/14/15
to golan...@googlegroups.com
So, i've checked my map,

map[whatEver:0x400b0]


funcMap["whatEver"]  =>
0x400b0

so key, val exists, and address returns. What else am i missing?


воскресенье, 14 июня 2015 г., 21:15:33 UTC+3 пользователь ksug написал:

Kiki Sugiaman

unread,
Jun 14, 2015, 4:16:37 PM6/14/15
to golan...@googlegroups.com
Well, yes. That's the address of the function.

But copy-paste of your snippet works just fine:
http://play.golang.org/p/WpnEXoF-6V

The reason for my first "auto-pilot" reply was because I've gotten used to passing regular map (when I don't care about efficiency) into Execute().
--

Alex Sinelnikov

unread,
Jun 14, 2015, 4:56:30 PM6/14/15
to golan...@googlegroups.com
I've found root of problem, here is description

https://groups.google.com/forum/#!topic/golang-nuts/vmZHHfLsH8M

воскресенье, 14 июня 2015 г., 23:16:37 UTC+3 пользователь ksug написал:

Ignacio Grande

unread,
Jun 17, 2015, 2:20:54 AM6/17/15
to golan...@googlegroups.com
I recently had exactly the same problem by the same reason. I found the answer in Stack Overflow: http://stackoverflow.com/questions/10199219/go-template-function

 I think that this behaviour should be better documented.
Reply all
Reply to author
Forward
0 new messages