Global variables

16,065 views
Skip to first unread message

John

unread,
Feb 10, 2010, 7:17:43 PM2/10/10
to golang-nuts
Perhaps I just overlooked this, but is there no way to declare global
variables?

Mike Ramirez

unread,
Feb 10, 2010, 7:29:37 PM2/10/10
to golan...@googlegroups.com

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

signature.asc

Ostsol

unread,
Feb 10, 2010, 8:40:54 PM2/10/10
to golang-nuts
The first letter of their names must also be capitalized. Even then,
a qualifier is necessary to access them. For example:
mypack.MyGlobal. Of course, you could import the package with an
explicit period beside the name, though if you are going to do that,
I'd put them in a special 'globals' package separate from everything
else. My own education has beaten into my head a practice of avoiding
the use of globals, though. . .

-Ostsol

>  signature.asc
> < 1KViewDownload

John

unread,
Feb 10, 2010, 8:44:28 PM2/10/10
to golang-nuts
Thank you both for responding.

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

Steven Blenkinsop

unread,
Feb 10, 2010, 8:50:37 PM2/10/10
to Mike Ramirez, golan...@googlegroups.com
Correct me if I'm wrong, but I'm pretty sure the universal block is things like make(), new(), and copy() : language built-ins.

Steven

unread,
Feb 10, 2010, 11:03:52 PM2/10/10
to golang-nuts
---------- Forwarded message ----------
From: Mike Ramirez <gu...il.com>
Date: Wed, Feb 10, 2010 at 9:11 PM
Subject: Re: [go-nuts] Global variables
To: Steven Blenkinsop <ste...il.com>

> 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
 
Hmm, I did consider packages, however, while they are within universal block, they need to be imported and are named at package level scope. It seems to me that the only things truly in universal namespace (diff from block) I'd say are built-in functions and types. It would be nice if somewhere in the legalese there was a breakdown of the philosophy of the universal block, other than saying it contains everything.

I've never seen any way of adding anything to the universal block other than packages, outside of altering the language... I also don't see any reason why you would either. 
signature.asc
Reply all
Reply to author
Forward
0 new messages