Re: [go-nuts] I cannot access global var from package. Any idea?

2018-11-19 Thread Juan Mamani
Thanks for comments.

Good idea is to implement "package qualifier".  I will try it.
(Conclusion:  Go's rule doesn't apply for subdirectories.  If I'm wrong let
me know=)

Thanks again!

El vie., 16 de nov. de 2018 a la(s) 10:53, Jan Mercl (0xj...@gmail.com)
escribió:

>
> On Fri, Nov 16, 2018 at 2:40 PM Juan Mamani 
> wrote:
>
> > Why can not access global var MyGlobalVar? Any idea?
>
> Go has no global scope. It has universe scope, but you cannot define
> anything there. `MyGlobalVar` has package scope. To access an exported
> identifier imported from pacakge foo, you must use the package qualifier,
> like in `foo.MyGlobalVar` - in the first approximation. For other options
> lookup "dot imports" but that's seldom a good choice.
>
>
> > According Golang docs should be possible.
>
> No.
>
> > It works when both files are in the same directory.
>
> Yes, package scope contains all TLD declarations in a package. visible
> everywhere in the package.
>
> > But when db.go is in another doesn't work.
>
> WAI
>
> --
>
> -j
>

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


Re: [go-nuts] I cannot access global var from package. Any idea?

2018-11-16 Thread Jan Mercl
On Fri, Nov 16, 2018 at 2:40 PM Juan Mamani 
wrote:

> Why can not access global var MyGlobalVar? Any idea?

Go has no global scope. It has universe scope, but you cannot define
anything there. `MyGlobalVar` has package scope. To access an exported
identifier imported from pacakge foo, you must use the package qualifier,
like in `foo.MyGlobalVar` - in the first approximation. For other options
lookup "dot imports" but that's seldom a good choice.


> According Golang docs should be possible.

No.

> It works when both files are in the same directory.

Yes, package scope contains all TLD declarations in a package. visible
everywhere in the package.

> But when db.go is in another doesn't work.

WAI

-- 

-j

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


[go-nuts] I cannot access global var from package. Any idea?

2018-11-16 Thread Juan Mamani

The sample code:

// main.go   in   varglobal/
package main
import(
"fmt"
"varglobal/db"
)

var MyGlobalVar  string ="Any value :)"

func main(){
db.TryDisplayMyVar()
}


// db.go   in varglobal/db/
package db

import "fmt"

func TryDisplayMyVar(){

fmt.Println("Value:",MyGlobalVar)
}

Results of compilation:
# varglobal/db
db/db.go:15:23: undefined: MyGlobalVar

Why can not access global var MyGlobalVar?  Any idea?
According  Golang docs   should be possible. It works when both files are 
in the same directory. But when db.go is in another doesn't work.

I tried this sample because I need to  update big bunch of code  
implementing one global variable from packages.  Some good samaritan out 
there?

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