go - Static local variable in golang -


is possible define local variable in golang can maintain value 1 function call another? in c, can using reserved word static.

example in c:

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

use closure:

function literals closures: may refer variables defined in surrounding function. variables shared between surrounding function , function literal, , survive long accessible.

it doesn't have in global scope, outside function definition.

func main() {      x := 1      y := func() {         fmt.println("x:", x)         x++     }      := 0; < 10; i++ {         y()     } } 

(sample on go playground)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -