I believe the `g` is only initialized after the declaration statement is
completed. So you can't refer to the `g` name because it isn't available
yet. You can do this:
func newGlobalOptions() *globalOptions {
ccv := ccvxml.New()
g := &globalOptions{
ccv: ccv,
funcMap: template.FuncMap{"List": ccv.List}
}
return g;
}
On Thu, Mar 9, 2017 at 7:33 PM DrGo <[email protected]> wrote:
> Given this declaration
> type globalOptions struct {
> ...
> ccv *ccvxml.CCV
> funcMap template.FuncMap
> }
>
> Why this does not compile
>
> //Initialize template functions
> func newGlobalOptions() *globalOptions {
> g := globalOptions{
> ccv: ccvxml.New(),
> funcMap: template.FuncMap{"List": g.ccv.List},
> //List: shortcut for ccv.List
> }
> return &g
> }
>
>
> Whereas this compiles
> func newGlobalOptions() *globalOptions {
> g := globalOptions{
> ccv: ccvxml.New(),
> }
> g.funcMap = template.FuncMap{"List": g.ccv.List}
> //List: shortcut for ccv.List
> return &g
> }
>
>
> Any cleaner way of achieving what I want which is to initialize two
> members of a struct referencing the first in the second member
> initialization?
>
> Cheers,
>
> --
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
*[]'s*
*Paulo Poiati*
blog.paulopoiati.com
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.