func (c *Chain) Write(in string) (int, error) {
	sr := strings.NewReader(in)
	p := make(Prefix, c.prefixLen)
	for {
		var s string
		if _, err := fmt.Fscan(sr, &s); err != nil {
			break
		}
		key := p.String()
		c.mu.Lock()
		c.Chain[key] = append(c.Chain[key], s)
		c.mu.Unlock()
		p.Shift(s)
	}
	return len(in), nil
}
Write parses the bytes into prefixes and suffixes that are stored in Chain.
package main
import (
	"encoding/gob"
	"fmt"
	"math/rand"
	"os"
	"strings"
	"sync"
)
type Prefix []string
func (p Prefix) String() string {
	return strings.Join(p, " ")
}
func (p Prefix) Shift(word string) {
	copy(p, p[1:])
	p[len(p)-1] = word
}
type Chain struct {
	Chain     map[string][]string
	prefixLen int
	mu        sync.Mutex
}
func NewChain(prefixLen int) *Chain {
	return &Chain{
		Chain:     make(map[string][]string),
		prefixLen: prefixLen,
	}
}