saving local variables within a function

3,535 views
Skip to first unread message

Ali Ali

unread,
Oct 18, 2012, 1:40:39 PM10/18/12
to golan...@googlegroups.com
Hi,

What would be an idiomatic Go way to save local variables between function calls? 
(Fortran way: labelling them "save" so that next time the function is entered, the values from previous calls to the function are there)?

--
ali

Dumitru Ungureanu

unread,
Oct 18, 2012, 1:49:03 PM10/18/12
to golan...@googlegroups.com
Closures, I think.

Szilard Covasa

unread,
Oct 18, 2012, 1:53:55 PM10/18/12
to golan...@googlegroups.com
Second that...check this example: http://tour.golang.org/#39 .

On Thursday, October 18, 2012 8:49:03 PM UTC+3, Dumitru Ungureanu wrote:
Closures, I think.

Szilard Covasa

unread,
Oct 18, 2012, 2:04:27 PM10/18/12
to golan...@googlegroups.com
Btw, looking on the wiki I found this example http://code.google.com/p/go-wiki/wiki/CommonMistakes "in the vein of" this discussion. Might be worth to check once you get the "feel" for closures.

yy

unread,
Oct 18, 2012, 2:12:26 PM10/18/12
to Ali Ali, golan...@googlegroups.com
On 18 October 2012 19:40, Ali Ali <alj...@gmail.com> wrote:
> Hi,
>
> What would be an idiomatic Go way to save local variables between function
> calls?

Instead of using a closure, you may create a struct with your
variables and make a method. If you don't want to expose that struct,
use an interface value. eg: http://play.golang.org/p/GAI9_1kmfT

> (Fortran way: labelling them "save" so that next time the function is
> entered, the values from previous calls to the function are there)?

(Usually, using save in Fortran is not a good idea.)


--
- yiyus || JGL .

Dumitru Ungureanu

unread,
Oct 18, 2012, 2:22:01 PM10/18/12
to golan...@googlegroups.com, Ali Ali
Is this addressing the idiomatic part too?


On Thursday, October 18, 2012 9:12:38 PM UTC+3, yiyus wrote:
Instead of using a closure, you may create a struct with your
variables and make a method. If you don't want to expose that struct,
use an interface value. eg: http://play.golang.org/p/GAI9_1kmfT

chris dollin

unread,
Oct 18, 2012, 2:24:58 PM10/18/12
to Ali Ali, golan...@googlegroups.com
Make the function a method on a struct that has those
variables as members. or give it a pointer-to-that-struct
argument.

Or, possibly, depending on why you want to save
state like that, run the function as a goroutine with output
down a channel.

Chris

--
Chris "allusive" Dollin

Andy Balholm

unread,
Oct 18, 2012, 2:27:40 PM10/18/12
to golan...@googlegroups.com, Ali Ali, ehog....@googlemail.com
Or, again depending on exactly what you're trying to do, use global variables instead of local ones :-). This is equivalent to using static variables in C; I'm not sure if it's the same as "save" in Fortran.

Dumitru Ungureanu

unread,
Oct 18, 2012, 2:35:49 PM10/18/12
to golan...@googlegroups.com, Ali Ali, ehog....@googlemail.com
This is, again, interesting, but it has the down effect (or maybe an up effect) of losing the shadowing capabilities for vars. Shadowing makes for cleaner to read code, once you get the hang of it.

Plus, it protects your creative side from being wasted on hunting for distinct names on global vars after global vars after global vars. :)

yy

unread,
Oct 18, 2012, 4:09:04 PM10/18/12
to Dumitru Ungureanu, golan...@googlegroups.com, Ali Ali
On 18 October 2012 20:22, Dumitru Ungureanu <itmi...@gmail.com> wrote:
> Is this addressing the idiomatic part too?
>> http://play.golang.org/p/GAI9_1kmfT

Well. If you have some data you want to modify with a function,
storing that data in a struct and defining the function as a method is
indeed idiomatic. Using interface values of non-exported types is also
quite idiomatic.

It is quite possible that a closure or a global variable is a better
option to solve your problem but defining types and methods may help
you to structure your package better, depending on your particular
case. Usually, the most idiomatic solution is the simplest one.

Dumitru Ungureanu

unread,
Oct 18, 2012, 4:29:49 PM10/18/12
to golan...@googlegroups.com, Dumitru Ungureanu, Ali Ali
Your example, the save struct isn't just a fancy way of using global variables?
Reply all
Reply to author
Forward
0 new messages