code: func prepFile(path string) (*countingWriter, error) {
  	f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0600)
  	if err != nil {
  		return nil, err
  	}
  	_, err = f.Seek(-1, io.SeekEnd)
  	if err != nil {
  		return nil, err
  	}
  	buf := make([]byte, 1)
  	var cut int64
  	for {
  		if _, err := f.Read(buf); err != nil {
  			return nil, err
  		}
  		if buf[0] == '\n' {
  			break
  		}
  		if _, err = f.Seek(-2, io.SeekCurrent); err != nil {
  			return nil, err
  		}
  		cut++
  	}
  	fi, err := f.Stat()
  	if err != nil {
  		return nil, err
  	}
  	ns := fi.Size() - cut
  	if err = f.Truncate(ns); err != nil {
  		return nil, err
  	}
  	return &countingWriter{w: f, count: uint(ns)}, nil
  }
docs: // prepFile opens the log file at the given path, and cuts off the invalid part
  // from the end, because the previous execution could have been finished by interruption.
  // Assumes that every line ended by '\n' contains a valid log record.