WIP: Go version #6
Conversation
This comment has been minimized.
This comment has been minimized.
Yes that is one way. I like to create a plural version of the type for a slice representation, in this case "Comparisons": type Comparisons []*Comparison Your do not need to to specify Asc, the sort interface provides a sort.Reverse type that you could use to sort in descending order: sort.Sort(sort.Reverse(ComparisonWhenAsc(comparisons))) Instead of implementing Len/Swap for each type of sorting you want to do you could also do this: type Comparisons []*Comparison
func (a Comparisons) Len() int {
return len(a)
}
func (a Comparisons) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
// Optional: implement a default Less() method...
// Implement a sort by When
type ComparisonsByWhen struct {
// this is an "embedded" field, the closest you'll get to "inheritance" in Go.
// Len() defined above will be used when ComparisonsByWhen.Len() is called (same for Swap())
Comparisons
}
func (a ComparisonsByWhen) Less(i, j int) bool {
return a.Comparisons[i].When().Unix() < a.Comparisons[j].When().Unix()
}
// Implement sort by Author (Author does not exists, just an example)
type ComparisonsByAuthor struct {
Comparisons
}
func (a ComparisonsByAuthor) Less(i, j int) bool {
return a.Comparisons[i].Author < a.Comparisons[j].Author
}
comparisons := make( Comparisons, 0 )
// fill in the Comparisons slice...
// and sort...
sort.Sort( ComparisonsByWhen{comparisons} )
sort.Sort( ComparisonsByAuthor{comparisons} )
sort.Sort( sort.Reverse(ComparisonsByWhen{comparisons}) )
sort.Sort( sort.Reverse(ComparisonsByAuthor{comparisons}) ) Essentially, that's what sort.Reverse does (https://golang.org/src/sort/sort.go?s=4988:5026#L205): type reverse struct {
// This embedded Interface permits Reverse to use the methods of
// another Interface implementation.
Interface
}
// Less returns the opposite of the embedded implementation's Less method.
func (r reverse) Less(i, j int) bool {
return r.Interface.Less(j, i)
}
// Reverse returns the reverse order for data.
func Reverse(data Interface) Interface {
return &reverse{data}
} |
This comment has been minimized.
This comment has been minimized.
BTW, I installed and compiled a version of this on Friday. I have to say that getting "github.com/libgit2/git2go" to work was more complex than normal third party Go libraries (even libraries with C code). |
Thanks for this, again. I just made some changes to the sorting. re: git2go and portability; I agree - it was tricky to get it going for me too. What did you have to do to get it going? It seems like it's a problem as soon as the version of libgit2 and git2go are out of sync. I had quite a bit of cleaning up to do to make sure it was picking up the right version of libgit2, but In all cases - I much prefer having libgit2 as a tricky dependency rather than having to worry about dependencies and compilers for the C version. |
Rewrite in Go for better portability.