osamingo / goss

Provides sorted-slice for golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GoSS

Build Status Coverage License

Description

GoSS provides sorted-slice for golang.

Installation

$ go get github.com/osamingo/goss

Quick start

package main

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
	mrand "math/rand"
	"sync"

	"github.com/osamingo/goss"
)

type Result struct {
	ID    string
	Score int
}

func (r *Result) Target() int64 {
	return int64(r.Score)
}

func (r *Result) Priority() string {
	return r.ID
}

func randStr() string {
	rb := make([]byte, 32)
	rand.Read(rb)
	return base64.URLEncoding.EncodeToString(rb)
}

func main() {

	rs := []*Result{}
	fmt.Println("# Before")
	for i := 0; i < 10; i++ {
		r := &Result{ID: randStr()}
		fmt.Printf("%2d: %v\n", i+1, r)
		rs = append(rs, r)
	}

	wg := new(sync.WaitGroup)
	wg.Add(len(rs))

	finChan := make(chan bool)
	retChan := make(chan *Result, len(rs))

	go func() {
		wg.Wait()
		finChan <- true
	}()

	for _, r := range rs {
		go func(res *Result) {
			defer wg.Done()
			res.Score = mrand.Intn(1000)
			retChan <- res
		}(r)
	}

	s := &goss.SortedSlice{DESC: true}

LOOP:
	for {
		select {
		case <-finChan:
			break LOOP
		case r := <-retChan:
			s.Add(r)
		}
	}

	fmt.Println("\n# After")
	for i, r := range s.S {
		fmt.Printf("%2d: %v\n", i+1, r)
	}

}

Tips

type Result struct {
	ID    string
	Score int
	
	TargetFunc   func() int64
	PriorityFunc func() string
}

func (r *Result) Target() int64 {
	return r.TargetFunc()
}

func (r *Result) Priority() string {
	return r.PriorityFunc()
}

License

MIT

About

Provides sorted-slice for golang

License:MIT License


Languages

Language:Go 100.0%