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.