...
sync lock
package main
import (
"fmt"
"sync"
)
type Item interface {
}
type ItemStack struct {
items []Item
lock sync.RWMutex
}
func NewStack() *ItemStack {
s := &ItemStack{}
s.items = []Item{}
return s
}
func (s *ItemStack) Print() {
fmt.Println(s.items)
}
func (s *ItemStack) Push(t Item) {
s.lock.Lock()
s.items = append(s.items, t)
s.lock.Unlock()
}
func (s *ItemStack) Pop() (item Item) {
s.lock.Lock()
defer s.lock.Unlock()
sl := len(s.items)
if sl == 0 {
return nil
}
item = s.items[sl-1]
s.items = s.items[0 : sl-1]
return item
}
func main() {
stack := NewStack()
stack.Print()
stack.Push(3)
stack.Print()
stack.Push(5)
stack.Print()
stack.Pop()
stack.Print()
stack.Pop()
stack.Print()
}
...
sort
package main
import (
"fmt"
"sort"
)
func main() {
intlist := [] int{2,3,8,5,6,7,4,9}
sort.Sort(sort.Reverse(sort.IntSlice(intlist)))
float8list := []float64{4.2,5.2,6.2,7.2,1.2}
sort.Sort(sort.Reverse(sort.Float64Slice(float8list)))
stringlist := []string{"a","b","e","d","c"}
sort.Sort(sort.Reverse(sort.StringSlice(stringlist)))
fmt.Printf("%v
%v
%v",intlist,float8list,stringlist)
}