This is a creation in Article, where the information may have evolved or changed.
Ring Buffer Algorithm benefits: High memory usage, in buffered buffer memory model, It is not easy to have memory out of bounds, dangling pointers and other bugs, the problem is also easy to analyze debugging at the memory level. The system is easy to keep strong.
package mainimport ("bytes" "FMT") type ringbuf struct {buf []bytestart, size int}func new (Size int) *Ringbuf {Return &ringbuf{make ([]byte, size), 0, 0}}func (r *ringbuf) Write (b []byte) {for len (b) > 0 {start := (r.start + r.size) % len (R.BUF) n := copy (r.buf[start:], b) B = b[n:] //golang is to make good use of the slice if r.size >= len (R.BUF) {if n <= len (R.BUF) {r.start += Nif r.start >= len (R.BUF) {r.start = 0}} else {r.start = 0}}r.size += n// size can ' t exceed the capacityif r.size > cap (R.BUF) {r.size = cap (r.buf)}}}func (r *ringbuf) Read (b []byte) int {read :=&Nbsp;0size := r.sizestart := r.startfor len (b) > 0 && Size > 0 {end := start + sizeif end > len (R.BUF) {End = len (R.BUF)}n := copy (B, r.buf[start:end]) size -= nread += nb = b[n:]start = (start + n) % len (R.BUF)}return read}func (R&NBSP;*RINGBUF) size () int {return r.size}