You should post a reproducer if you want a real answer; a playground link 
or little repo.

On Tuesday, March 10, 2026 at 1:10:24 PM UTC-3 andreas graeper wrote:

> hi. i see a video on youtube about cobra (cli)
> in a package c
>  c/c.go   // root-command
>  c/s.go   // sub-command
>
> --c.go-
>  var c = &cobra.Command {}
>
> --s.go--
>  var s = &cobra.Command{
>  }
>  func init(){
>   c.AddCommand( s, .. ) 
>  }
> go-build ok
> but if i run the program it crashes and i am told that s poinzrt is nil or 
> invalid. 
> so i thought that func init() is package-initialiser and maybe s is not 
> initialized, so i left declaration 
>  var s * command.Cobra = nil // explicit set to nil    
> in package-scope and moved initialization into init function 
>
> func init () {
>   s = & cobra.Command { .. } 
>   if s!=nil and c!=nil {
>   c.AddCommand(s)
>   } else { panic("nilpointer") } 
>  } 
> but in the end, not the s was invalid but the c. 
>
> if there is a package-directory c/ and a.go b.go c.go all in same 'package 
> c' are they initialized in alphabetic order ? or if there is an 
> init-function (i.e in b.go ) using a symbol from a.go, is then the 
> init-function in a.go called before ?  
>
> why initializing a pointer in package-scope is done before (success), but 
> inside init-function in the order the source-files are handled (crash) ? 
>
> thanks in advance, andi 
>

-- 
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].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/f76b22f7-6cf7-45b4-98fd-56b5aaaa7bc0n%40googlegroups.com.

Reply via email to