如何在GO中实现静态局部变量

发布于 2025-01-25 21:15:37 字数 600 浏览 1 评论 0原文

我和Go一起学习曲线。我喜欢:-)

好吧,我想实现这样的静态局部变量以避免声明全球。但是我在封闭中挣扎。我只想在函数首次调用时仅打印一条消息,而不是其他时间。

这是我的

func append(path string, vars Vars) {

    // raise a warning only once
    func() (func()){
        var f bool
        return func(){
            if len(vars["hostlurl"]) == 0 && !f {
                f = true
                fmt.Println("Warning: missing hosturl.")
            }
        }
    }()

    // append
    ...

}

代码中的代码本地代码如果Len(...从未称为

避免添加全局变量的方法?

谢谢您的帮助

I'm in the learning curve with go. I like like :-)

Well, I would like to implement such a static local variable to avoid to declare a global one. But I struggle with closure. I would like to print a message only the first time the function is called, and not the other times.

Here is my code

func append(path string, vars Vars) {

    // raise a warning only once
    func() (func()){
        var f bool
        return func(){
            if len(vars["hostlurl"]) == 0 && !f {
                f = true
                fmt.Println("Warning: missing hosturl.")
            }
        }
    }()

    // append
    ...

}

In this code the local code if len(... is never called

Is there a way to avoid to add a global variable?

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2025-02-01 21:15:37

GO中没有“静态局部变量”。

如果功能需要状态,则有很多选择。在 Go Playground

您可以使用软件包级别变量:

var state bool

func f1() {
    if !state {
        state = true
        fmt.Println("f1() first")
    }
    fmt.Println("f1() called")
}

测试:

f1()
f1()
// Output
f1() first
f1() called
f1() called

或将(指针转到)作为参数:

func f2(state *bool) {
    if !*state {
        *state = true
        fmt.Println("f2() first")
    }
    fmt.Println("f2() called")
}

测试:

var state bool
f2(&state)
f2(&state)
// Output
f2() first
f2() called
f2() called

或者您可以使用方法,并且状态可以存储在接收器中:

type foo struct {
    state bool
}

func (v *foo) f3() {
    if !v.state {
        v.state = true
        fmt.Println("foo.f3() first")
    }
    fmt.Println("foo.f3() called")
}

测试:

v := foo{}
v.f3()
v.f3()
// Output
foo.f3() first
foo.f3() called
foo.f3() called

或使用 sync.once.once 也是并发安全:

var f2Once sync.Once

func f4() {
    f2Once.Do(func() {
        fmt.Println("f4() first")
    })
    fmt.Println("f4() called")
}

测试:

f4()
f4()
// Output
f4() first
f4() called
f4() called

或返回a closure /em>指的是局部变量:

func f5() func() {
    var state bool
    return func() {
        if !state {
            state = true
            fmt.Println("f5() first")
        }
        fmt.Println("f5() called")
    }
}

测试:

fret := f5()
fret()
fret()
// Output
f5() first
f5() called
f5() called

您也可以使用函数变量,将a closure 分配给它:

var f6 = func() func() {
    var state bool
    return func() {
        if !state {
            state = true
            fmt.Println("f6() first")
        }
        fmt.Println("f6() called")
    }
}()

测试:

f6()
f6()
// Output
f6() first
f6() called
f6() called

您也可以使用方法值作为函数:

type bar struct {
    state bool
}

func (v *bar) f7() {
    if !v.state {
        v.state = true
        fmt.Println("foo.f7() first")
    }
    fmt.Println("foo.f7() called")
}

var f7 = (&bar{}).f7

测试:

f7()
f7()
// Output
f7() first
f7() called
f7() called

There are no "static local variables" in Go.

If a function needs a state, you have numerous options. Try all on the Go Playground.

You may use a package level variable:

var state bool

func f1() {
    if !state {
        state = true
        fmt.Println("f1() first")
    }
    fmt.Println("f1() called")
}

Testing:

f1()
f1()
// Output
f1() first
f1() called
f1() called

Or pass the (pointer to) state as an argument:

func f2(state *bool) {
    if !*state {
        *state = true
        fmt.Println("f2() first")
    }
    fmt.Println("f2() called")
}

Testing:

var state bool
f2(&state)
f2(&state)
// Output
f2() first
f2() called
f2() called

Or you may use a method and the state may be stored in the receiver:

type foo struct {
    state bool
}

func (v *foo) f3() {
    if !v.state {
        v.state = true
        fmt.Println("foo.f3() first")
    }
    fmt.Println("foo.f3() called")
}

Testing:

v := foo{}
v.f3()
v.f3()
// Output
foo.f3() first
foo.f3() called
foo.f3() called

Or use sync.Once which is also concurrency safe:

var f2Once sync.Once

func f4() {
    f2Once.Do(func() {
        fmt.Println("f4() first")
    })
    fmt.Println("f4() called")
}

Testing:

f4()
f4()
// Output
f4() first
f4() called
f4() called

Or return a closure that refers to a local variable:

func f5() func() {
    var state bool
    return func() {
        if !state {
            state = true
            fmt.Println("f5() first")
        }
        fmt.Println("f5() called")
    }
}

Testing:

fret := f5()
fret()
fret()
// Output
f5() first
f5() called
f5() called

You may also use a function variable, assigning a closure to it:

var f6 = func() func() {
    var state bool
    return func() {
        if !state {
            state = true
            fmt.Println("f6() first")
        }
        fmt.Println("f6() called")
    }
}()

Testing:

f6()
f6()
// Output
f6() first
f6() called
f6() called

You may also use a method value as a function:

type bar struct {
    state bool
}

func (v *bar) f7() {
    if !v.state {
        v.state = true
        fmt.Println("foo.f7() first")
    }
    fmt.Println("foo.f7() called")
}

var f7 = (&bar{}).f7

Testing:

f7()
f7()
// Output
f7() first
f7() called
f7() called
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文