Go语言中的静态局部变量

huangapple go评论106阅读模式
英文:

Static local variable in Go

问题

在Go语言中,可以使用var关键字定义一个局部变量,但是它的值在函数调用之间是不会保持的。Go语言中没有类似C语言中的static关键字来实现这种功能。如果你想要在函数调用之间保持变量的值,可以考虑使用全局变量或者将变量作为函数的参数传递。

英文:

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:

<!-- language: c -->

  1. int func() {
  2. static int x = 0;
  3. x++;
  4. return x;
  5. }

答案1

得分: 38

使用闭包:

函数字面量是闭包:它们可以引用在周围函数中定义的变量。这些变量在周围函数和函数字面量之间共享,并且只要它们可访问,它们就会一直存在。

它不必在全局范围内,只需在函数定义之外。

  1. func main() {
  2. x := 1
  3. y := func() {
  4. fmt.Println("x:", x)
  5. x++
  6. }
  7. for i := 0; i < 10; i++ {
  8. y()
  9. }
  10. }

(在 Go Playground 上的示例)

英文:

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.

  1. func main() {
  2. x := 1
  3. y := func() {
  4. fmt.Println(&quot;x:&quot;, x)
  5. x++
  6. }
  7. for i := 0; i &lt; 10; i++ {
  8. y()
  9. }
  10. }

(Sample on the Go Playground)

答案2

得分: 23

你可以这样做

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. f := do()
  7. f() // 1
  8. f() // 2
  9. }
  10. func do() (f func()){
  11. var i int
  12. f = func(){
  13. i++
  14. fmt.Println(i)
  15. }
  16. return
  17. }

Playground链接:https://play.golang.org/p/D9mv9_qKmN

英文:

You can do something like this

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func main() {
  6. f := do()
  7. f() // 1
  8. f() // 2
  9. }
  10. func do() (f func()){
  11. var i int
  12. f = func(){
  13. i++
  14. fmt.Println(i)
  15. }
  16. return
  17. }

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

答案3

得分: 20

在全局范围声明一个变量:

  1. var i = 1
  2. func a() {
  3. println(i)
  4. i++
  5. }
英文:

Declare a var at global scope:

  1. var i = 1
  2. func a() {
  3. println(i)
  4. i++
  5. }

答案4

得分: 3

像Taric的建议一样,但是staticCounter()返回一个int函数

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func staticCounter() (f func() int) {
  6. var i int
  7. f = func() int {
  8. i++
  9. // fmt.Println(i)
  10. return i
  11. }
  12. return
  13. }
  14. func main() {
  15. f := staticCounter()
  16. g := staticCounter()
  17. fmt.Println(f())
  18. fmt.Println(f())
  19. fmt.Println(f())
  20. fmt.Println(f())
  21. fmt.Println(g())
  22. fmt.Println(g())
  23. }
英文:

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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func staticCounter() (f func()(int)){
  6. var i int
  7. f = func()(int){
  8. i++
  9. // fmt.Println(i)
  10. return i
  11. }
  12. return
  13. }
  14. func main() {
  15. f := staticCounter()
  16. g := staticCounter()
  17. fmt.Println(f())
  18. fmt.Println(f())
  19. fmt.Println(f())
  20. fmt.Println(f())
  21. fmt.Println(g())
  22. fmt.Println(g())
  23. }

答案5

得分: 3

在以下示例中,变量sum在每个闭包a1和a2中都像一个独立的静态变量一样。

  1. package main
  2. import "fmt"
  3. func adder() func(int) int {
  4. sum := 0
  5. return func(x int) int {
  6. sum += x
  7. return sum
  8. }
  9. }
  10. func main() {
  11. a1, a2 := adder(), adder()
  12. for i := 0; i < 10; i++ {
  13. fmt.Println(
  14. a1(i),
  15. a2(-1*i),
  16. )
  17. }
  18. }

输出结果:

  1. 0 0
  2. 1 -1
  3. 3 -3
  4. 6 -6
  5. 10 -10
  6. 15 -15
  7. 21 -21
  8. 28 -28
  9. 36 -36
  10. 45 -45
英文:

Use Function closure

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

  1. package main
  2. import &quot;fmt&quot;
  3. func adder() func(int) int {
  4. sum := 0
  5. return func(x int) int {
  6. sum += x
  7. return sum
  8. }
  9. }
  10. func main() {
  11. a1,a2 := adder(), adder()
  12. for i := 0; i &lt; 10; i++ {
  13. fmt.Println(
  14. a1(i),
  15. a2(-1*i),
  16. )
  17. }
  18. }

Output

  1. 0 0
  2. 1 -1
  3. 3 -3
  4. 6 -6
  5. 10 -10
  6. 15 -15
  7. 21 -21
  8. 28 -28
  9. 36 -36
  10. 45 -45

答案6

得分: 1

在所有其他答案中,包含静态变量的函数是在主函数中赋值的。

以下是如何在全局范围内定义和赋值该函数的方法。

  1. var myFunction = func() func(type1, type2) type3 {
  2. myStaticVariable := []string {"hello", "world"}
  3. return func(arg1 type1, arg2 type2) type3 {
  4. // 在这里使用 arg1、arg2 和 myStaticVariable
  5. }
  6. }()
英文:

In all the other answers, the function containing a static variable is assigned in the main.

Here is how you can define and assign that function in the global scope.

  1. var myFunction = func() func(type1, type2) type3 {
  2. myStaticVariable := []string {&quot;hello&quot;, &quot;world&quot;}
  3. return func(arg1 type1, arg2 type2) type3 {
  4. // use arg1, arg2 and myStaticVariable here
  5. }
  6. }()

答案7

得分: 0

// 一个变量x1是局部变量,不是全局变量。
// 静态变量是不能从其他函数中访问的,就像全局变量一样。
// 静态变量在函数结束时不会消失。
// 这个程序中的x1和x2就是这样假装的。

包 main

import (
"fmt"
)

/*
int func() { // 函数内的静态变量x。
static int x = 0;
x++;
return x;
}
*/

//

func main() {

  1. //
  2. var x1 int = 0
  3. var x2 int = 100
  4. //
  5. for i := 0; i < 10; i++ { // 调用一个“静态”变量x
  6. x1 = fun1(&x1)
  7. x2 = fun2(&x2)
  8. fmt.Printf("%d %d \n", x1, x2)
  9. } //
  10. test1(x1, x2) // 一个函数需要参数来看到x1和x2

} //main

//

func fun1(p *int) int {
//
*p++ // 保存值

  1. return *p // 计数器x1

}

//

func fun2(p *int) int {

  1. *p++ // 保存值
  2. return *p // 计数器x2

}

//

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

英文:
  1. // A var x1 is local to main(), is not a global var.
  2. // A static var is one that can&#39;t be accesed from others functions just
  3. // like global vars.
  4. // A static var dont disappears when the function ends.
  5. // So is what x1 n x2 are pretending in this program.
  6. package main
  7. import (
  8. &quot;fmt&quot;
  9. )
  10. /*
  11. int func() { // x static inside a function.
  12. static int x = 0;
  13. x++;
  14. return x;
  15. }
  16. */
  17. //
  18. func main() {
  19. //
  20. var x1 int = 0
  21. var x2 int = 100
  22. //
  23. for i := 0; i &lt; 10; i++ { // call to a &quot;static&quot; var x
  24. x1 = fun1(&amp;x1)
  25. x2 = fun2(&amp;x2)
  26. fmt.Printf(&quot;%d %d \n&quot;, x1, x2)
  27. } //
  28. test1(x1, x2) // a funct needs parameters to see x1 n x2
  29. } //main
  30. //
  31. func fun1(p *int) int {
  32. //
  33. *p++ // save value
  34. return *p //counter x1
  35. }
  36. //
  37. func fun2(p *int) int {
  38. *p++ // save value
  39. return *p //counter x2
  40. }
  41. //
  42. func test1(x1 int, x2 int) {
  43. fmt.Println(&quot;\&quot;x1\&quot; y \&quot;x2\&quot; &quot;, x1, x2)
  44. }

huangapple
  • 本文由 发表于 2015年5月31日 21:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/30558071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定