code: func OpenWriteSegment(logger log.Logger, dir string, k int) (*Segment, error) {
  	segName := SegmentName(dir, k)
  	f, err := os.OpenFile(segName, os.O_WRONLY|os.O_APPEND, 0666)
  	if err != nil {
  		return nil, err
  	}
  	stat, err := f.Stat()
  	if err != nil {
  		f.Close()
  		return nil, err
  	}
  	// If the last page is torn, fill it with zeros.
  	// In case it was torn after all records were written successfully, this
  	// will just pad the page and everything will be fine.
  	// If it was torn mid-record, a full read (which the caller should do anyway
  	// to ensure integrity) will detect it as a corruption by the end.
  	if d := stat.Size() % pageSize; d != 0 {
  		level.Warn(logger).Log("msg", "last page of the wal is torn, filling it with zeros", "segment", segName)
  		if _, err := f.Write(make([]byte, pageSize-d)); err != nil {
  			f.Close()
  			return nil, errors.Wrap(err, "zero-pad torn page")
  		}
  	}
  	return &Segment{File: f, i: k, dir: dir}, nil
  }
docs: // OpenWriteSegment opens segment k in dir. The returned segment is ready for new appends.