github.com/fufuok/balancer@v1.0.0/examples/random/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/fufuok/balancer" 7 ) 8 9 func main() { 10 nodes := []string{"A", "B", "C"} 11 // lb := balancer.New(balancer.Random, nodes) 12 lb := balancer.NewRandom(nodes) 13 fmt.Println("balancer name:", lb.Name()) 14 15 // random 16 for i := 0; i < 9; i++ { 17 fmt.Print(lb.Select(), " ") 18 } 19 fmt.Println() 20 21 // add an item to be selected 22 lb.Add("E") 23 fmt.Println(lb.Select()) 24 25 // get all items 26 nodes = lb.All().([]string) 27 // [A B C E] 28 fmt.Printf("%+v\n", nodes) 29 30 nodes = append(nodes, "F") 31 // reinitialize the balancer items 32 lb.Update(nodes) 33 34 // random 35 for i := 0; i < 7; i++ { 36 fmt.Print(lb.Select(), " ") 37 } 38 fmt.Println() 39 40 // remove an item 41 lb.Remove("E") 42 43 // remove all items 44 lb.RemoveAll() 45 }