go - Golang Antlr 找不到解析器的路径
问题描述
我正在尝试使用 Antlr 生成的 Golang 解析器。但是当我尝试在我的主文件中访问它时,它给了我以下错误:build command-line-arguments: cannot find module for path path/to/parser
我的主文件是这样的:
package main
import (
"fmt"
"./parser"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
func main() {
fmt.Println("Hello, World!")
is := antlr.NewInputStream("1 + 2 * 3")
// Create the Lexer
lexer := parser.NewHelloWorldLexer(is)
// Read all tokens
for {
t := lexer.NextToken()
if t.GetTokenType() == antlr.TokenEOF {
break
}
fmt.Printf("%s (%q)\n",
lexer.SymbolicNames[t.GetTokenType()], t.GetText())
}
}
解决方案
这是使用 go 模块的解决方案
你的项目结构应该是这样的
.
├── go.mod
├── main.go
└── parser
└── parser.go
去.mod
module example.com/projectname
go 1.15
...
main.go
package main
import (
"fmt"
"example.com/projectname/parser"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
...
解析器
package parser
import (
"github.com/antlr/antlr4/runtime/Go/antlr"
)
...
推荐阅读
- python - 我想销毁我按下的按钮,但它显示找不到按钮的错误(python tkinter)
- oauth-2.0 - Microsoft oAuth 2.0 实施发生了哪些变化?
- django - Tabulator PUT 通过 Ajax 到 Django REST 端点 - 将表减少到最后编辑的记录
- visual-c++ - 链表中的访问冲突读取位置 0xCCCCCCCC
- view - 在 userContentController 中启动/更新视图 - 功能
- docker - 在 Google Cloud 中部署 Docker 映像时 ENTRYPOINT 无效
- c# - Linq groupby orderby 和 join 一起
- javascript - 脚本被错误的复选框触发的问题
- apache-kafka - Kafka commitTransaction 确认失败
- flutter - RenderListWheelViewport 对象在布局期间被赋予无限大小