New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Go version #6

merged 18 commits into from Jun 20, 2015


None yet
2 participants

vroy commented Jun 6, 2015

Rewrite in Go for better portability.


This comment has been minimized.

sergeleger commented on gb.go in bdace8b Jun 7, 2015

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:


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())

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 {

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 (

type reverse struct {
    // This embedded Interface permits Reverse to use the methods of
    // another Interface implementation.

// 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.

sergeleger commented on bdace8b Jun 7, 2015

BTW, I installed and compiled a version of this on Friday. I have to say that getting "" to work was more complex than normal third party Go libraries (even libraries with C code).


This comment has been minimized.


vroy commented Jun 7, 2015

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 brew install libgit2 is the final step I had to do.

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.

vroy added a commit that referenced this pull request Jun 20, 2015

@vroy vroy merged commit f5882a8 into master Jun 20, 2015

@vroy vroy deleted the go branch Jun 21, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment