Static local variable in Go

2020-06-07 04:06发布

问题:

Is it possible to define a local variable in Go that can maintain its value from one function call to another? In C, we can do this using the reserved word static.

Example in C:

int func() {
    static int x = 0; 
    x++;
    return x;
}

回答1:

Use a closure:

Function literals are closures: they may refer to variables defined in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.

It doesn't have to be in global scope, just outside the function definition.

func main() {

    x := 1

    y := func() {
        fmt.Println("x:", x)
        x++
    }

    for i := 0; i < 10; i++ {
        y()
    }
}

(Sample on the Go Playground)



回答2:

Declare a var at global scope:

var i = 1

func a() {
  println(i)
  i++
}


回答3:

You can do something like this

package main

import (
    "fmt"
)

func main() {
  f := do()
  f() // 1
  f() // 2
}

func do() (f func()){
  var i int
  f = func(){
    i++
    fmt.Println(i)
  }
  return
}

Link on Playground https://play.golang.org/p/D9mv9_qKmN



回答4:

Like Taric' suggestion, but with staticCounter() returning an int function

package main

import (
    "fmt"
)

func staticCounter() (f func()(int)){
  var i int
  f = func()(int){
    i++
//  fmt.Println(i)
    return i
  }
  return
}

func main() {
  f := staticCounter()
  g := staticCounter()
  fmt.Println(f())
  fmt.Println(f())
  fmt.Println(f())
  fmt.Println(f())
  fmt.Println(g())
  fmt.Println(g())
}


回答5:

Use Function closure

In following example, variable sum behaves like a separate static for each closure a1 and a2.

package main

import "fmt"

func adder() func(int) int {
    sum := 0
    return func(x int) int {
        sum += x
        return sum
    }
}

func main() {
    a1,a2 := adder(), adder()
    for i := 0; i < 10; i++ {
        fmt.Println(
            a1(i),
            a2(-1*i),
        )
    }
}

Output

0 0
1 -1
3 -3
6 -6
10 -10
15 -15
21 -21
28 -28
36 -36
45 -45


回答6:

// A var x1 is local to main(), is not a global var.
// A static var is one that can't be accesed from others functions just
// like global vars.
// A static var dont disappears when the function ends.
// So is what x1 n x2 are pretending in this program.
package main

import (
    "fmt"
)

/*
int func() {     // x static inside a function.
    static int x = 0;
    x++;
    return x;
}
*/

// 

func main() {

    //
    var x1 int = 0
    var x2 int = 100
    //

    for i := 0; i < 10; i++ {           // call to a "static" var x

        x1 = fun1(&x1)
        x2 = fun2(&x2)

        fmt.Printf("%d     %d \n", x1, x2)

    } //

    test1(x1, x2) // a funct needs parameters to see x1 n x2

} //main

//

func fun1(p *int) int {
    //
    *p++ // save value

    return *p //counter  x1
}

//

func fun2(p *int) int {

    *p++      // save value
    return *p //counter x2
}

//

func test1(x1 int, x2 int) {
    fmt.Println("\"x1\" y \"x2\"  ", x1, x2)
}


标签: go