FAQ
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

--

Search Discussions

  • Dumitru Ungureanu at Oct 18, 2012 at 6:48 pm
    Closures, I think.

    --
  • Szilard Covasa at Oct 19, 2012 at 12:38 am
    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 at Oct 18, 2012 at 11:19 pm
    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.
    On Thursday, October 18, 2012 8:53:55 PM UTC+3, Szilard Covasa wrote:

    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.
    --
  • Yy at Oct 18, 2012 at 7:49 pm

    On 18 October 2012 19:40, Ali Ali 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 at Oct 18, 2012 at 7:28 pm
    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
    --
    - yiyus || JGL .
    --
  • Yy at Oct 18, 2012 at 8:09 pm

    On 18 October 2012 20:22, Dumitru Ungureanu wrote:
    Is this addressing the idiomatic part too?
    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.


    --
    - yiyus || JGL .

    --
  • Dumitru Ungureanu at Oct 18, 2012 at 10:25 pm
    Your example, the save struct isn't just a fancy way of using global
    variables?

    --
  • Chris dollin at Oct 18, 2012 at 8:09 pm

    On 18 October 2012 18:40, Ali Ali wrote:
    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)?
    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 at Oct 18, 2012 at 8:20 pm
    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 at Oct 18, 2012 at 7:41 pm
    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. :)
    On Thursday, October 18, 2012 9:27:40 PM UTC+3, Andy Balholm wrote:

    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.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 18, '12 at 6:46p
activeOct 19, '12 at 12:38a
posts11
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase