FAQ
Hi,

First post - apologies if this is the wrong place/noob question

It doesn't seem as though pointers are correctly evaluated for comparisons
in templates?
A pointer to an integer of the value of 0 will equate to True, simply
because the pointer itself is present the value (0) will not be considered.

From my understanding of the docs <http://golang.org/pkg/text/template/>, a
nil pointer should be False and a valid pointer uses the actual value for
comparison.

If I am mistaken and this is the intended functionality - how can I
properly work with pointers in templates?

Some approximation of a test:

*home.html*
{{if .TotalCount}}
This should not appear because total count is {{.TotalCount}} - but it does
because the pointer points somewhere.
{{end}}

*main.go*
package main

import (
     "net/http"
     "html/template"
)

type page_context struct {
     TotalCount *int
}

func PageHandler(response http.responseWriter, request *http.Request) {
     total_count := 0
     context := page_context{&total_count}
     page, _ := template.ParseFiles("home.html")
     page.Execute(response, context)
}

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Rob Pike at Oct 24, 2014 at 6:30 pm
    Working as intended. A nil pointer evaluates to false, a non-nil pointer
    to true.

    It's trivial to add a function to your FuncMap to give you the semantics
    you desire.

    -rob

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 24, '14 at 6:18p
activeOct 24, '14 at 6:30p
posts2
users2
websitegolang.org

2 users in discussion

Bor3ham: 1 post Rob Pike: 1 post

People

Translate

site design / logo © 2021 Grokbase