mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
fs
This commit is contained in:
19
fs/fs.go
Normal file
19
fs/fs.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsExist(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsDir(path string) bool {
|
||||||
|
file, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.IsDir()
|
||||||
|
}
|
37
fs/temps.go
Normal file
37
fs/temps.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TempFileWithText(text string) (*os.File, error) {
|
||||||
|
return TempFile([]byte(text))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TempFilenameWithText(text string) (string, error) {
|
||||||
|
tmpfile, err := TempFileWithText(text)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := tmpfile.Name()
|
||||||
|
if err = tmpfile.Close(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return filename, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TempFile(data []byte) (*os.File, error) {
|
||||||
|
tmpfile, err := ioutil.TempFile(os.TempDir(), "tmp*")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ioutil.WriteFile(tmpfile.Name(), data, os.ModeTemporary); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmpfile, nil
|
||||||
|
}
|
Reference in New Issue
Block a user