59 lines
1022 B
Go
59 lines
1022 B
Go
|
package data
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
type Article struct {
|
||
|
Title string
|
||
|
Created time.Time
|
||
|
Desc string
|
||
|
Content string
|
||
|
}
|
||
|
|
||
|
type ArticleList struct {
|
||
|
addChan chan Article
|
||
|
releaseChan chan int
|
||
|
returnChan chan Article
|
||
|
List []Article
|
||
|
}
|
||
|
|
||
|
func (l *ArticleList) start() {
|
||
|
for {
|
||
|
select {
|
||
|
case article := <-l.addChan:
|
||
|
l.List = append(l.List, article)
|
||
|
case i := <-l.releaseChan:
|
||
|
l.returnChan <- l.List[i]
|
||
|
l.List = append(l.List[:i], l.List[i+1:]...)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewArticleList() *ArticleList {
|
||
|
list := &ArticleList{
|
||
|
List: []Article{},
|
||
|
addChan: make(chan Article),
|
||
|
releaseChan: make(chan int),
|
||
|
returnChan: make(chan Article),
|
||
|
}
|
||
|
list.start()
|
||
|
return list
|
||
|
}
|
||
|
|
||
|
func (l *ArticleList) Close() {
|
||
|
close(l.addChan)
|
||
|
close(l.releaseChan)
|
||
|
close(l.returnChan)
|
||
|
}
|
||
|
|
||
|
func (l *ArticleList) Add(a Article) {
|
||
|
l.addChan <- a
|
||
|
}
|
||
|
|
||
|
func (l *ArticleList) Release(i int) (Article, bool) {
|
||
|
if i < 0 || i > len(l.List) {
|
||
|
return Article{}, false
|
||
|
}
|
||
|
l.releaseChan <- i
|
||
|
return <-l.returnChan, true
|
||
|
}
|