它失败了go run
or go test
(编译然后运行),但不是go build
(仅编译)。我会认为MustCompile
与编译有关,而不是运行时。
package main
import (
"regexp"
)
var someInvalidRegex = regexp.MustCompile(`(?!`)
func main() {
someInvalidRegex.MatchString("foo")
}
运行时失败:
$ go run main.go
panic: regexp: Compile(`(?!`): error parsing regexp: invalid or unsupported Perl syntax: `(?!`
goroutine 1 [running]:
regexp.MustCompile(0x10b7d19, 0x3, 0xc420022070)
/usr/local/Cellar/go/1.10.3/libexec/src/regexp/regexp.go:240 +0x171
exit status 2
编译成功:
$ go build -o foo
$ echo $?
0
运行时再次失败:
$ ./foo
panic: regexp: Compile(`(?!`): error parsing regexp: invalid or unsupported Perl syntax: `(?!`
goroutine 1 [running]:
regexp.MustCompile(0x10b7d19, 0x3, 0xc420022070)
/usr/local/Cellar/go/1.10.3/libexec/src/regexp/regexp.go:240 +0x171