code: func NewWriter(fn string) (*Writer, error) {
  	dir := filepath.Dir(fn)
  
  	df, err := fileutil.OpenDir(dir)
  	if err != nil {
  		return nil, err
  	}
  	defer df.Close() // Close for platform windows.
  
  	if err := os.RemoveAll(fn); err != nil {
  		return nil, errors.Wrap(err, "remove any existing index at path")
  	}
  
  	f, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0666)
  	if err != nil {
  		return nil, err
  	}
  	if err := df.Sync(); err != nil {
  		return nil, errors.Wrap(err, "sync dir")
  	}
  
  	iw := &Writer{
  		f:     f,
  		fbuf:  bufio.NewWriterSize(f, 1<<22),
  		pos:   0,
  		stage: idxStageNone,
  
  		// Reusable memory.
  		buf1:    encoding.Encbuf{B: make([]byte, 0, 1<<22)},
  		buf2:    encoding.Encbuf{B: make([]byte, 0, 1<<22)},
  		uint32s: make([]uint32, 0, 1<<15),
  
  		// Caches.
  		symbols:       make(map[string]uint32, 1<<13),
  		seriesOffsets: make(map[uint64]uint64, 1<<16),
  		crc32:         newCRC32(),
  	}
  	if err := iw.writeMeta(); err != nil {
  		return nil, err
  	}
  	return iw, nil
  }
docs: // NewWriter returns a new Writer to the given filename. It serializes data in format version 2.