在使用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 }
如上,该问题解决