ds3016 2018-07-31 14:56 采纳率: 100%
浏览 693
已采纳

为什么要为regexp.MustCompile解析错误进行构建成功?

It fails for go run or go test (compile then run), but not for go build (compile only). I would have thought MustCompile relates to compilation, not runtime.


package main

import (
    "regexp"
)

var someInvalidRegex = regexp.MustCompile(`(?!`)

func main() {
    someInvalidRegex.MatchString("foo")
}

Runtime fail:

$ 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

Compilation success:

$ go build -o foo
$ echo $?
0

Runtime fail again:

$ ./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
  • 写回答

2条回答 默认 最新

  • dongqi0644 2018-07-31 15:00
    关注

    Compiler does not analyze your regular expression. It is done in runtime. "Compile" part of "MustCompile" function name stands for the compilation of the regular expression.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献