etree.go:801: undefined: strings.Compare

在使用https://github.com/beevik/etree.git 的etree时,出现问题 etree.go:801: undefined: strings.Compare

查找 了一下 strings.go 文件:

sudo find / | grep strings.go

使用vim 查看 了一下该文件,

vim /usr/share/go/pkg/strings/strings.go

然后在vim中查找 Compare,提示

E486: Pattern not found: Compare

也就是说 strings.Compare() 并没有被定义,但是 go文档中有相关的函数,这可能是由当前使用的 go 版本太低导致的。

解决 这个问题,可以 将当前的go 更新至最新版本。不过有点麻烦,考虑到 etree.go 只使用了两次 strings.Compare() 函数,并且它的源代码也很简单:

func Compare(a, b string) int {
    // NOTE(rsc): This function does NOT call the runtime cmpstring function,
    // because we do not want to provide any performance justification for
    // using strings.Compare. Basically no one should use strings.Compare.
    // As the comment above says, it is here only for symmetry with package bytes.
    // If performance is important, the compiler should be changed to recognize
    // the pattern so that all code doing three-way comparisons, not just code
    // using strings.Compare, can benefit.
    if a == b {
        return 0
    }
    if a < b {
        return -1
    }
    return +1
}

所以我采用的方式是更改 etree.go 代码。
旧代码

800 func (a byAttr) Less(i, j int) bool {
801         sp := strings.Compare(a[i].Space, a[j].Space)
802         if sp == 0 {
803                 return strings.Compare(a[i].Key, a[j].Key) < 0
804         }
805         return sp < 0
806 }

新代码

 799 func strings_Compare(a, b string) int {
 800         if a == b {
 801                 return 0
 802         }
 803         if a < b {
 804                 return -1
 805         }
 806         return +1
 807 }
 808 func (a byAttr) Less(i, j int) bool {
 809         sp := strings_Compare(a[i].Space, a[j].Space)
 810         if sp == 0 {
 811                 return strings_Compare(a[i].Key, a[j].Key) < 0
 812         }
 813         return sp < 0
 814 }

如上,该问题解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值