如何在Windows上使用基于cgo的包?

huangapple go评论139阅读模式
英文:

How do I use a cgo-based package on Windows?

问题

Go语言标准库中的regexp功能相对较弱,因此我需要一个更强大的引擎,比如Python中的正则表达式(pip install regex),支持递归、反向引用、前后查找等功能。

我找到了以下几个选项:
https://godoc.org/github.com/dlclark/regexp2
这是一个与.NET兼容的库,非常不错;但是,递归功能无法正常工作。

还有一些与PCRE绑定的选项,例如:
https://godoc.org/github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre

那么,我该如何在Win64上使用这个绑定呢?

英文:

The regexp in the Go's standard library is quite poor, so I need a more powerful engine, like regex in Python (pip install regex), supporting recursion, backref, look-ahead/behind, etc... .

I found:
https://godoc.org/github.com/dlclark/regexp2
.NET compatible, which was quite fine; however, recursion is not working properly.

and several bindings to PCRE, for example:
https://godoc.org/github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre

so, how can I use this binding on Win64?

答案1

得分: 1

你可以考虑使用C++标准库std::regex(无需第三方库)。将逻辑包装在try块中,使用catch(...){return ERROR;}捕获任何错误,并声明C函数extern "C",这样你就可以使用cgo调用。


从https://github.com/golang/go/wiki/cgo(有关Windows的部分):

> 为了在Windows上使用cgo,您还需要首先安装gcc编译器(例如mingw-w64),并将gcc.exe(等等)添加到PATH环境变量中,然后才能使用cgo进行编译。


话虽如此,我仍然认为你应该考虑继续使用regexp包,并尽量使正则表达式尽可能简单。因为复杂的正则表达式往往会影响代码的可读性。另一个问题是它们有时会引入难以察觉和修复的微妙错误。因此,相比于正则表达式,用Go编写更多的代码实际上可能会更容易。

英文:

You may consider using C++ standard library std::regex (no third-party library). Wrap the logic in try block, use catch(...){return ERROR;} to catch any error, and declare the C function extern "C" so you can call with cgo.


From https://github.com/golang/go/wiki/cgo (there is a part about Windows):

> In order to use cgo on Windows, you'll also need to first install a
> gcc compiler (for instance, mingw-w64) and have gcc.exe (etc.) in your
> PATH environment variable before compiling with cgo will work.


That being said, I still think you should consider sticking with the regexp package and try to make regular expressions as simple as possible. Because complicated regular expressions are likely to hurt readability of code. Another problem is sometimes they introduce subtle bugs which are difficult to spot and fix. So writing more code in Go instead of regex may actually make the life easier.

huangapple
  • 本文由 发表于 2017年2月25日 21:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/42456388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定