Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b7801dc550 | |||
| fc61b53643 |
53
FileUtils.go
Normal file
53
FileUtils.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package golanghelper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type FileUtils struct {
|
||||
}
|
||||
|
||||
func (f *FileUtils) CopyFile(srcFilePath, destFilePath string, isOverwrite bool) (err error) {
|
||||
var srcFileInfo os.FileInfo
|
||||
srcFileInfo, err = os.Stat(srcFilePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !srcFileInfo.Mode().IsRegular() {
|
||||
return errors.New(fmt.Sprintf("%s is not a regular file", srcFileInfo.Name()))
|
||||
}
|
||||
|
||||
var isNotExist bool
|
||||
_, err = os.Stat(destFilePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
isNotExist = true
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var destFile *os.File
|
||||
if isNotExist == false && isOverwrite == false {
|
||||
return errors.New("File already exists")
|
||||
}
|
||||
|
||||
destFile, err = os.Create(destFilePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
sourceFile, err := os.Open(srcFilePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer sourceFile.Close()
|
||||
|
||||
_, err = io.Copy(destFile, sourceFile)
|
||||
|
||||
return
|
||||
}
|
||||
25
JSONObject.go
Normal file
25
JSONObject.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package golanghelper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type JSONObject struct {
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func NewJSONObject(object interface{}) (jsonObject JSONObject, err error) {
|
||||
var data []byte
|
||||
data, err = json.Marshal(object)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
jsonObject = JSONObject{Data: data}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (j *JSONObject) ToString() string {
|
||||
return string(j.Data)
|
||||
}
|
||||
Reference in New Issue
Block a user