Grouped global variable/constant initialization?

3,394 views
Skip to first unread message

Péter Szilágyi

unread,
Mar 20, 2013, 11:48:33 AM3/20/13
to golang-nuts
Hi,

  If I have a group of global variables, like: http://play.golang.org/p/xPcgNTzD_- , how can I initialize them inline without having an extra function call. I know it could be done with init(), but I'd rather say something like:

const/var config struct {
  Str = "abc"
  Num = 123
}

Is there a way I'm missing to specify the above?

Thanks,
  Peter

PS: Basically what I'm trying to implement is a trivial global configuration space to put all "hard coded" values so that I don't litter my whole project with them and then try ang figure out where I put one thing or another.

Jan Mercl

unread,
Mar 20, 2013, 11:50:56 AM3/20/13
to Péter Szilágyi, golang-nuts
On Wed, Mar 20, 2013 at 4:48 PM, Péter Szilágyi <pet...@gmail.com> wrote:
> If I have a group of global variables, like:
> http://play.golang.org/p/xPcgNTzD_- , how can I initialize them inline
> without having an extra function call. I know it could be done with init(),
> but I'd rather say something like:
>
> const/var config struct {
> Str = "abc"
> Num = 123
> }
>
> Is there a way I'm missing to specify the above?

http://play.golang.org/p/TXbsveA9ZI

-j

Maxim Khitrov

unread,
Mar 20, 2013, 11:53:33 AM3/20/13
to Péter Szilágyi, golang-nuts
On Wed, Mar 20, 2013 at 11:48 AM, Péter Szilágyi <pet...@gmail.com> wrote:
> Hi,
>
> If I have a group of global variables, like:
> http://play.golang.org/p/xPcgNTzD_- , how can I initialize them inline
> without having an extra function call. I know it could be done with init(),
> but I'd rather say something like:
>
> const/var config struct {
> Str = "abc"
> Num = 123
> }
>
> Is there a way I'm missing to specify the above?

Almost:

http://play.golang.org/p/lWAgW5Hf-8

- Max

Péter Szilágyi

unread,
Mar 20, 2013, 11:57:40 AM3/20/13
to Maxim Khitrov, golang-nuts
Ah, so *that's* where the = goes :))... thanks!

Kevin Gillette

unread,
Mar 20, 2013, 12:55:30 PM3/20/13
to golan...@googlegroups.com, Péter Szilágyi
A hypothetical "inferred struct type" would seem useful in these areas (note: this code doesn't work)... http://play.golang.org/p/sapiYqaU5J
Reply all
Reply to author
Forward
0 new messages