func (lw *LazyFile) Write(p []byte) (int, error) {
	if lw.file == nil {
		err := os.MkdirAll(path.Dir(lw.FileName), 0755)
		if err != nil {
			return 0, err
		}
		lw.file, err = os.Create(lw.FileName)
		if err != nil {
			return 0, err
		}
	}
	return lw.file.Write(p)
}
Write writes to the specified file and creates the file first time it is called.
package out
import (
	"os"
	"path"
)
type LazyFile struct {
	FileName string
	file     *os.File
}
func (lw *LazyFile) Close() error {
	if lw.file != nil {
		return lw.file.Close()
	}
	return nil
}