On Wednesday 10 February 2010 16:17:43 John wrote:
> Perhaps I just overlooked this, but is there no way to declare global
> variables?
>
Yes, just declare them outside of any functions, like this:
import "fmt"
var test string = "test"
func fun() {
fmt.Printf("%s\n", test)
return
}
The wording in the spec[1] might be a bit vague, but the second rule refers to this.
"The scope of an identifier denoting a constant, type, variable, or function declared at top level (outside any function) is the package block."
This is about as global as you're going to get, there is the universal block (rule 1), but in practice, I haven't used any globals outside of the package block.
Mike
[1] http://golang.org/doc/go_spec.html#Declarations_and_scope
--
The mosquito is the state bird of New Jersey.
-- Andy Warhol
-Ostsol
> signature.asc
> < 1KViewDownload
And yes, Ostsol, I certainly do not like globals either. I'm just
trying to develop a fuller knowledge of the language's capabilities.
Thanks,
John
> Correct me if I'm wrong, but I'm pretty sure the universal block is
> things like make(), new(), and copy() : language built-ins.
>
Techinically no, the universe block encompases all Go source text. see
http://golang.org/doc/go_spec.html#Blocks
The built-ins live at this level and I can't think of a way off the top of my
head how to use the universe block in such a fashion. I don't even worry
about it much, I use the package block as my top most block, I wouldn't even
suggest using the universe block unless you really know what you're doing.
Mike
--
Personally, I don't often talk about social good because when I hear other
people talk about social good, that's when I reach for my revolver.
-- Eric Raymond