Go: Global variables

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

Go: Global variables

问题

我可以通过在任何函数外部定义对象来轻松地使其成为全局对象,但对于某些必须创建的结构体来说,这种方法不起作用。

我试图做的是一个更复杂的版本:

package main
import "regexp"

func doSomething(test []byte) []byte {
   test = reg.ReplaceAll(test,nil)
   return test
}

reg, _ := regexp.Compile(`[^a-z\s]`) // 这是问题所在

func main() {
   thing := doSomething("somebytes")
}

显然,上述代码是不允许的,但这正是我想要做的。

似乎没有办法在doSomething函数内部访问reg对象,而我又不想传递它,因为我将要执行这个操作数十亿次。

如果我将它放在main()函数中,它就不再是全局的了。我甚至尝试过这样做:

var reg regexp.Regexp
func main() {
   reg, _ = regexp.Compile(`[^a-z\s]`)
   thing := doSomething("somebytes")
}

...但这也不起作用,它给我一个错误。

有什么想法吗?

更新:我的问题实际上与正则表达式无关,那只是一个例子。

英文:

I can easily make an object global by defining it outside of any function, but this does not work for certain structs that must be created.

What I'm trying to do is a much more complicated version of this:

package main
import "regexp"

func doSomething(test []byte) []byte {
   test = reg.ReplaceAll(test,nil)
   return test
}

reg, _ := regexp.Compile(`[^a-z\s]`) // this is the issue

func main() {
   thing := doSomething("somebytes")
}

Obviously the above is not allowed, but that's what I'd like to do.

There does not appear to be any way to make that reg object accessible from within the doSomething function without passing it, which I want to avoid because I'm going to be doing this several billion times.

If I put it in main() then it's no longer global. I've even tried this:

var reg regexp.Regexp
func main() {
   reg, _ = regexp.Compile(`[^a-z\s]`)
   thing := doSomething("somebytes")
}

...but that doesn't work either, it gives me an error.

Any ideas?

Update: My issue is not actually with regexp. That was an example.

答案1

得分: 12

可以这样使用:

var reg = regexp.MustCompile(`[^a-z\s]`) //或者
var reg, _ = regexp.Compile(`[^a-z\s]`) //不过,MustCompile 是更好的选择。

还要记住`regexp.Compile` / `regexp.MustCompile` 返回一个指针这就是为什么你的第二次尝试没有成功你将 `*regexp.Regexp` 赋值给了 `regexp.Regexp`

完整的示例

func doSomething(test []byte) []byte {
    test = reg.ReplaceAll(test, nil)
    return test
}

var (
    reg, _ = regexp.Compile(`[^a-z\s]`)
    reg2   *regexp.Regexp
)

func init() { //初始化全局变量的不同方式
    reg2 = regexp.MustCompile(`[^a-z\s]`)
}

func main() {
    thing := doSomething([]byte("somebytes"))
    fmt.Println(thing)
}

希望对你有帮助!

英文:

It's allowed just fine, you can use:

var reg = regexp.MustCompile(`[^a-z\s]`) //or even
var reg, _ = regexp.Compile(`[^a-z\s]`) // MustCompile is the better choice though.

Also keep in mind that regexp.Compile / regexp.MustCompile returns a pointer, and that's why your 2nd attempt didn't work, you were assigning *regexp.Regexp to regexp.Regexp.

Full example:

func doSomething(test []byte) []byte {
	test = reg.ReplaceAll(test, nil)
	return test
}

var (
	reg, _ = regexp.Compile(`[^a-z\s]`)
	reg2   *regexp.Regexp
)

func init() { //different way of initializing global valuables
	reg2 = regexp.MustCompile(`[^a-z\s]`)
}

func main() {
	thing := doSomething([]byte("somebytes"))
	fmt.Println(thing)
}

答案2

得分: 2

短声明语法

reg, _ := regexp.Compile(`[^a-z\s]`)

在全局范围内是不允许的。请明确使用var代替。

var reg, _ = regex.Compile(`[^a-z\s]`)
英文:

The short declaration syntax

reg, _ := regexp.Compile(`[^a-z\s]`)

is not allowed for global scope. Use var explicitly instead.

var reg, _ = regex.Compile(`[^a-z\s]`)

huangapple
  • 本文由 发表于 2014年8月2日 23:40:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/25096688.html
匿名

发表评论

匿名网友

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

确定