#Access global vars in main from seperate packages?

16 messages · Page 1 of 1 (latest)

fringe sinew
#

I was watching this video where all this guy did was declare a global variable outside his main function and was able to access that anywhere. I'm doing the exact same thing and it's not working. Maybe there is something underlying im missing?

mighty yew
#

Global vars bad

#

But also you can't import package main, so that will not work as is

fringe sinew
#

im not sure how that guy was able to do it then; what would you suggest instead?

mighty yew
#

Passing things where you need them to be

#

dependency injection

fringe sinew
#

ill look into that, thank you!

vagrant bone
#

Names that start with a capital letter are exported

fringe sinew
#

and in his video it wasnt even capitalized and worked

real carbon
#

maybe because all of it is in the same package?

#

if that's the case you can access it freely

#

if the whole thing is main you could had accessed global vars from anywhere
but in this case it's all fragmented so you cant

#

consider creating a struct instead of using package level functions,
where the struct holds the redis client, so by holding the struct you can call struct.func instead

slate wadiBOT
shut cave
#

Last one here goes over a few ways you can do this