예제 #1
0
파일: cache.go 프로젝트: stvvan/golibs
func size(c *cache.Cache) {
	m := &runtime.MemStats{}
	runtime.ReadMemStats(m)
	fmt.Printf("Cache size: %v\n", ansi.Color(fmt.Sprintf("%v", c.Size()), ansi.Red))
	fmt.Printf("Memory Acquired: %v\n", ansi.Color(fmt.Sprintf("%v", m.Sys), ansi.Blue))
	fmt.Printf("Memory Used: %v\n", ansi.Color(fmt.Sprintf("%v", m.Alloc), ansi.Yellow))
}
예제 #2
0
파일: image.go 프로젝트: tjyang/golibs
func main() {
	file, err := os.Open("./flowers.jpg")
	defer file.Close()

	if err != nil {
		log.Printf("EachPixel Test failed: %v", err)
	}
	img, _ := graphics.EachPixel(file, func(r, g, b, a uint8) (uint8, uint8, uint8, uint8) {
		fmt.Printf("r: %v\tg: %v\tb: %v\n", ansi.Color(r, ansi.Red), ansi.Color(g, ansi.Green), ansi.Color(b, ansi.Blue))
		return uint8(255 - r), uint8(255 - g), uint8(255 - b), a
	})
	fd, err := os.Create("./inv.jpg")
	if err != nil {
		log.Printf("EachPixel Test failed: %v", err)
	}

	err = jpeg.Encode(fd, img, nil)
	if err != nil {
		log.Printf("EachPixel Test failed: %v", err)
	}

	err = fd.Close()
	if err != nil {
		log.Printf("EachPixel Test failed: %v", err)
	}
}
예제 #3
0
파일: all.go 프로젝트: stvvan/golibs
func main() {
	floats := []float64{1.4, 3.14, 9.81, 13.2, 23.42, 33.7, 44.11, 51}
	array := stack.Lifo()
	array.Push(fmt.Sprintf("Lorem %v", ansi.Color("Ipsum", ansi.Blue)))
	array.Push(fmt.Sprintf("Dolor %v", ansi.Bold("sit Amet")))
	array.Push(fmt.Sprintf("5th Prime: %v", xmath.Prime(5)))
	array.Push(fmt.Sprintf("\n\tMin: %v\n\tMax: %v\n\tMedian: %v\n\tArithmetic: %v\n\tHarmonic: %v\n\tGeometric: %v", xmath.Min(floats), xmath.Max(floats), xmath.Median(floats), xmath.Arithmetic(floats), xmath.Harmonic(floats), xmath.Geometric(floats)))
	array.Push(fmt.Sprintf("Date: %v", as.Time("11.01.2015")))
	for array.Len() > 0 {
		log.Println(array.Pop())
	}

	array.Push(string(as.Bytes(regex.ReplaceAllString("foobar", "o+", "u"))))
	array.Push(as.Int(23.0000))
	array.Push(as.Float(13.37))
	array.Push(as.String(23.0))
	array.Push(as.Bool(111111))
	array.Push(as.Bytes(12.34))
	array.Push(as.String(strconv.ParseInt("42", 10, 0)))
	array.Push(as.FloatFromXString("2,3"))
	array.Push(as.FloatFromXString(".23"))
	array.Push(as.String("\r\n\t\n"))
	for array.Len() > 0 {
		log.Println(array.Pop())
	}
}
예제 #4
0
파일: ring.go 프로젝트: tjyang/golibs
func main() {
	r := stack.Ring()
	r.Init(10)
	r.SetSize(15)
	go func() {
		for {
			rand.Seed(time.Now().UnixNano())
			time.Sleep(time.Duration(rand.Intn(4)) * time.Second)
			rand.Seed(time.Now().UnixNano())
			ran := as.String(rand.Intn(127))
			pos := r.Push(ran)
			log.Printf("Value %v added at %v\n", ansi.Bold(ansi.Color(ran, ansi.Red)), ansi.Bold(ansi.Color(as.String(pos), ansi.Blue)))
		}
	}()
	var last int = 0
	var nlast int = 0
	var vals []string
	var i int

	for {
		_, err := fmt.Scanf("%d", &i)
		if err != nil {
			fmt.Println("only numbers")
		} else {
			ix := as.String(i)
			nlast = r.Push(ix)
			log.Printf("Value %v added at %v\n", ansi.Bold(ansi.Color(ix, ansi.Green)), ansi.Bold(ansi.Color(as.String(nlast), ansi.Blue)))
			log.Printf("Get from %v\n", last)
			vals = r.Get(last)
			for _, v := range vals {
				if v != "" {
					log.Printf("\t%v\n", ansi.Color(as.String(v), ansi.Yellow))
				}
			}
			last = nlast
		}
	}
}
예제 #5
0
파일: cache.go 프로젝트: stvvan/golibs
func main() {
	c := cache.New(10*time.Second, 1000*time.Millisecond)
	size(c)

	log.Println("writing first 500 values")
	for i := 0; i < 500; i++ {
		rand.Seed(time.Now().UnixNano())
		time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)

		rand.Seed(time.Now().UnixNano())
		ran := as.String(rand.Intn(999))

		c.Add(fmt.Sprintf("Value %v", i), ran)
	}
	size(c)

	log.Println("delete random values")
	for i := 0; i < 100; i++ {
		c.Delete(fmt.Sprintf("Value %v", c.Get(fmt.Sprintf("Value %v", i))))
	}
	size(c)

	log.Println("writing next 500 values")
	for i := 500; i < 1000; i++ {
		rand.Seed(time.Now().UnixNano())
		time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)

		rand.Seed(time.Now().UnixNano())
		ran := as.String(rand.Intn(999))

		c.Add(fmt.Sprintf("Value %v", i), ran)
	}
	size(c)

	log.Println("delete random values")
	for i := 200; i < 300; i++ {
		c.Delete(fmt.Sprintf("Value %v", c.Get(fmt.Sprintf("Value %v", i))))
	}
	size(c)

	log.Println("output all values")
	for i := 0; i < 1000; i++ {
		v := c.Get(fmt.Sprintf("Value %v", i))
		if v == nil {
			fmt.Printf("%v: \t%v\t", i, v)
		} else {
			fmt.Printf("%v: \t%v\t", i, ansi.Color(fmt.Sprintf("%v", v), ansi.Green))
		}

		if i%5 == 0 {
			fmt.Println()
		}
	}
	fmt.Println()
	size(c)
	log.Println("wait 11 seconds")
	time.Sleep(time.Duration(11) * time.Second)
	size(c)
	log.Println("run gc")
	runtime.GC()
	size(c)
	log.Println("wait 11 seconds")
	time.Sleep(time.Duration(11) * time.Second)
	size(c)

	runt()
}