Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template does not dereference pointers to values (ints / strings) #1478

Closed
gopherbot opened this issue Feb 3, 2011 · 3 comments
Closed

template does not dereference pointers to values (ints / strings) #1478

gopherbot opened this issue Feb 3, 2011 · 3 comments

Comments

@gopherbot
Copy link
Contributor

by mhantsch:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?

max@mahako:~/src/test$ cat test1.go
package main

import "os"
import "io"
import "template"

type Node struct {
    Label string
    Value1 *int
    Value2 int
}

const tplStr = `Node {Label}:
    Value1={.section Value1}{@}{.or}(unset){.end}
    Value2={Value2}
`

var tpl = template.MustParse(tplStr, nil)

func (n *Node) Show(w io.Writer) (err os.Error) {
    err = tpl.Execute(n, w)
    return err
}

func main() {
        var n Node
        var i int = 33
        n.Label = "michi"
        n.Value1 = &i
        n.Value2 = 44
        n.Show(os.Stderr)
}

max@mahako:~/src/test$ 8g test1.go ; 8l -o test1 test1.8
max@mahako:~/src/test$ ./test1
Node michi:
    Value1=0x35b9f8
    Value2=44

What is the expected output?

max@mahako:~/src/test$ ./test1
Node michi:
    Value1=33
    Value2=44

What do you see instead?

max@mahako:~/src/test$ ./test1
Node michi:
    Value1=0x35b9f8
    Value2=44

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
Windows 7 (386) and Ubuntu Linux (386)

Which revision are you using?  (hg identify)
867d37fb41a4+ release/release.2011-02-01.1

Please provide any additional information below.
Apparently template does not dereference pointers to ints and strings, it only follows
pointers to structs. I believe it should automatically deref others also.
Alternatively, there could be some syntax for dereferencing, for example {*@} ...?
@rsc
Copy link
Contributor

rsc commented Feb 4, 2011

Comment 1:

I think the auto dereference might be an interesting thing
for the default formatter to do, but it probably shouldn't for
user-installed formatters, which might be formatting pointers
to custom structs.

Owner changed to r...@golang.org.

Status changed to Accepted.

@robpike
Copy link
Contributor

robpike commented Feb 4, 2011

Comment 2:

I was thinking along those lines but I'm not sure it's easy to do efficiently.  The
default formatter does whatever Fprint does, which means that the dereferencing should
stop as soon as we reach a level that has a String method.  That's a lot of reflection
for every value.
In short, it's easy but clumsy and expensive. Is it worth it?

@robpike
Copy link
Contributor

robpike commented Feb 4, 2011

Comment 3:

This issue was closed by revision 5008c63.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants