天天看點

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 < 
  }
           

如上,該問題解決