FAQ
I'm having trouble defining a variable to hold a pre-compiled regexp.

I can compile a regexp expression and store it in a variable using := but I
can't pre-define the variable.

This code works fine.
------
package main

import (
"regexp"
)

func main() {
re := regexp.MustCompile("foo")
re.Match( []byte("bar") )
}
---------------



This code reports Regexp undefined
$ go run test1.go
# command-line-arguments
./test1.go:8: undefined: Regexp
---------------

package main

import (
"regexp"
)

func main() {
var re *Regexp
re = regexp.MustCompile("foo")
re.Match( []byte("bar") )
}

------------------------------


Where did I go wrong?

Keith


--

Search Discussions

  • Thomas Bushnell, BSG at Nov 1, 2012 at 4:25 pm
    You need regexp.Regexp as the type.

    Thomas
    On Nov 1, 2012 5:15 PM, "Keith Mcqueen" wrote:


    I'm having trouble defining a variable to hold a pre-compiled regexp.

    I can compile a regexp expression and store it in a variable using := but
    I can't pre-define the variable.

    This code works fine.
    ------
    package main

    import (
    "regexp"
    )

    func main() {
    re := regexp.MustCompile("foo")
    re.Match( []byte("bar") )
    }
    ---------------



    This code reports Regexp undefined
    $ go run test1.go
    # command-line-arguments
    ./test1.go:8: undefined: Regexp
    ---------------

    package main

    import (
    "regexp"
    )

    func main() {
    var re *Regexp
    re = regexp.MustCompile("foo")
    re.Match( []byte("bar") )
    }

    ------------------------------


    Where did I go wrong?

    Keith


    --

    --
  • Keith Mcqueen at Nov 1, 2012 at 4:32 pm
    Thank you! I'm a raw noob, can you tell?

    On Thursday, November 1, 2012 10:25:20 AM UTC-6, Thomas Bushnell, BSG wrote:

    You need regexp.Regexp as the type.

    Thomas
    On Nov 1, 2012 5:15 PM, "Keith Mcqueen" <kmcq...@gmail.com <javascript:>>
    wrote:
    I'm having trouble defining a variable to hold a pre-compiled regexp.

    I can compile a regexp expression and store it in a variable using := but
    I can't pre-define the variable.

    This code works fine.
    ------
    package main

    import (
    "regexp"
    )

    func main() {
    re := regexp.MustCompile("foo")
    re.Match( []byte("bar") )
    }
    ---------------



    This code reports Regexp undefined
    $ go run test1.go
    # command-line-arguments
    ./test1.go:8: undefined: Regexp
    ---------------

    package main

    import (
    "regexp"
    )

    func main() {
    var re *Regexp
    re = regexp.MustCompile("foo")
    re.Match( []byte("bar") )
    }

    ------------------------------


    Where did I go wrong?

    Keith


    --

    --
  • Minux at Nov 1, 2012 at 4:25 pm

    On Thu, Nov 1, 2012 at 11:45 PM, Keith Mcqueen wrote:

    This code reports Regexp undefined
    $ go run test1.go
    # command-line-arguments
    ./test1.go:8: undefined: Regexp
    ---------------

    package main

    import (
    "regexp"
    )

    func main() {
    var re *Regexp
    var re *regexp.Regexp
    re = regexp.MustCompile("foo")
    re.Match( []byte("bar") )
    }
    --
  • Matt Kane's Brain at Nov 1, 2012 at 4:34 pm
    You need to prepend the package name.

    var re *regexp.Regexp

    --
    matt kane's brain
    http://hydrogenproject.com

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 1, '12 at 4:15p
activeNov 1, '12 at 4:34p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase