func (w *FileWriter) Close() (err error) {
	w.Lock()
	defer w.Unlock()
	if err = w.rotate(); err != nil {
		return err
	}
	w.current = nil
	return nil
}
Close closes the current file.  Writer is unusable after this
is called.
package rotate
import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"
)
const (
	FilePerm   = os.FileMode(0644)
	DateFormat = "2006-01-02"
)
type FileWriter struct {
	filename                 string
	filenameRotationTemplate string
	current                  *os.File
	interval                 time.Duration
	maxSizeInBytes           int64
	maxArchives              int
	expireTime               time.Time
	bytesWritten             int64
	sync.Mutex
}
func NewFileWriter(filename string, interval time.Duration, maxSizeInBytes int64, maxArchives int) (io.WriteCloser, error) {
	if interval == 0 && maxSizeInBytes <= 0 {
		return openFile(filename)
	}
	w := &FileWriter{
		filename:                 filename,
		interval:                 interval,
		maxSizeInBytes:           maxSizeInBytes,
		maxArchives:              maxArchives,
		filenameRotationTemplate: getFilenameRotationTemplate(filename),
	}
	if err := w.openCurrent(); err != nil {
		return nil, err
	}
	return w, nil
}
func openFile(filename string) (*os.File, error) {
	return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, FilePerm)
}
func getFilenameRotationTemplate(filename string) string {
	fileExt := filepath.Ext(filename)
	stem := strings.TrimSuffix(filename, fileExt)
	return stem + ".%s-%s" + fileExt
}
func (w *FileWriter) Write(p []byte) (n int, err error) {
	w.Lock()
	defer w.Unlock()
	if n, err = w.current.Write(p); err != nil {
		return 0, err
	}
	w.bytesWritten += int64(n)
	if err = w.rotateIfNeeded(); err != nil {
		return 0, err
	}
	return n, nil
}