Little problem with global variables

1,060 views
Skip to first unread message

Carlos E. Muñoz

unread,
Apr 20, 2016, 3:07:49 PM4/20/16
to golang-nuts
Hi, maybe it's a noob error but I can't find the solution.

This is the situation, I've a global variable in my main package and I need to change the value with other package.

Maybe this little code can explain the issue a bit better.

-------------------------------------
package main

import "Control"

var JumpFirst bool = false

func main{
         //Proceso con JumpFirst
}

-------------------------------------

package Control

func ChangeFlag (){
        JumpFirst = true
}

Shawn Milochik

unread,
Apr 20, 2016, 3:46:55 PM4/20/16
to golang-nuts
On Wed, Apr 20, 2016 at 10:49 AM, Carlos E. Muñoz <sna...@gmail.com> wrote:
Hi, maybe it's a noob error but I can't find the solution.

This is the situation, I've a global variable in my main package and I need to change the value with other package.

Maybe this little code can explain the issue a bit better


Why do you think you need to change a global variable in package "main" from an imported package? Imported packages can never be expected to know about things in main.

You can do what you want by (in main) doing: JumpFirst = Control.ChangeFlag()
And have ChangeFlag return a value.

Also, package names are lower-case by convention.

keith....@gmail.com

unread,
Apr 20, 2016, 7:37:55 PM4/20/16
to golang-nuts, Sh...@milochik.com
package Control

import "main"

func ChangeFlag (){
        main.JumpFirst = true

Jakob Borg

unread,
Apr 21, 2016, 3:19:00 AM4/21/16
to golang-nuts, sna...@gmail.com
The main package is not importable. If it were, you'd get an import
cycle when using any package that did so. There is no way for a
non-main package to access variables in the main package (other than
by passing them a pointer to one, etc).

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

Art Mellor

unread,
Apr 21, 2016, 8:30:44 AM4/21/16
to golang-nuts
You can make a sub-package for globals and import that, e.g.

$GOPATH/mypkg/main.go //package main
$GOPATH/mypkg/globals/globals.go // package globals
$GOPATH/otherpkg/other.go // package otherpkg; import "mypkg/globals"

msilv...@cloudflare.com

unread,
Apr 21, 2016, 11:42:48 AM4/21/16
to golang-nuts
That's not a good approach.

Your main package should consume other packages. The "upstream" package—the one imported by main—should have a way to configure things (via a constructor, exported package variable, config struct, etc).

e.g.

package main


func main() {
     opts := &yourotherpkg.Options{Tokens: true, ID: "qwerty"}
     thing, err := yourotherpkg.New(opts)
     if err != nil {
          log.Fatal(err)
     }

     // Use thing

Carlos E. Muñoz

unread,
Apr 21, 2016, 12:05:41 PM4/21/16
to golang-nuts
Thanks all for your answers, I already solve the problem was a really noob error. 

I had two functions one in main and the other and my custom package, I just move the function in main into my package and also the flags. As it should be from the beginning.
Reply all
Reply to author
Forward
0 new messages