https://github.com/osamingo/goss
Provides sorted-slice for golang
https://github.com/osamingo/goss
Last synced: over 1 year ago
JSON representation
Provides sorted-slice for golang
- Host: GitHub
- URL: https://github.com/osamingo/goss
- Owner: osamingo
- License: mit
- Created: 2015-01-06T08:23:09.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-10-24T20:43:00.000Z (over 7 years ago)
- Last Synced: 2025-01-04T13:44:21.376Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 164 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
GoSS
====
[](https://travis-ci.org/osamingo/goss)
[](https://codecov.io/github/osamingo/goss)
[](https://github.com/osamingo/goss/blob/master/LICENSE)
## Description
GoSS provides sorted-slice for golang.
## Installation
```
$ go get github.com/osamingo/goss
```
## Quick start
```go
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
```go
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