天天看点

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中查找

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 
    }
    if a < b {
        return -
    }
    return +
}
           

所以我采用的方式是更改

etree.go

代码。

旧代码:

func (a byAttr) Less(i, j int) bool {
         sp := strings.Compare(a[i].Space, a[j].Space)
         if sp ==  {
                 return strings.Compare(a[i].Key, a[j].Key) < 
         }
         return sp < 
 }
           

新代码:

func strings_Compare(a, b string) int {
          if a == b {
                  return 
          }
          if a < b {
                  return -
          }
          return +
  }
  func (a byAttr) Less(i, j int) bool {
          sp := strings_Compare(a[i].Space, a[j].Space)
          if sp ==  {
                  return strings_Compare(a[i].Key, a[j].Key) < 
          }
          return sp < 
  }
           

如上,该问题解决