@@ -55,7 +55,7 @@ func ReadLinesV2(path string) ([]string, int, error) {
5555 }
5656}
5757
58- // ListDir lists all the file or dir names in the specified directory.
58+ // ListDir lists all the file or directory names in the specified directory.
5959// Note that ListDir don't traverse recursively.
6060func ListDir (dirname string ) ([]string , error ) {
6161 infos , err := ioutil .ReadDir (dirname )
@@ -69,12 +69,12 @@ func ListDir(dirname string) ([]string, error) {
6969 return names , nil
7070}
7171
72- // IsPathExist checks whether a file/dir exists.
72+ // IsExist checks whether a file/dir exists.
7373// Use os.Stat to get the info of the target file or dir to check whether exists.
7474// If os.Stat returns nil err, the target exists.
7575// If os.Stat returns a os.ErrNotExist err, the target does not exist.
7676// If the error returned is another type, the target is uncertain whether exists.
77- func IsPathExist (path string ) (bool , error ) {
77+ func IsExist (path string ) (bool , error ) {
7878 _ , err := os .Stat (path )
7979 if err == nil {
8080 return true , nil
@@ -123,17 +123,19 @@ func IsFileE(path string) (bool, error) {
123123 return false , err
124124}
125125
126- // IsSymlink checks a file whether is a symbolic link.
126+ // IsSymlink checks a file whether is a symbolic link on Linux .
127127// Note that this doesn't work for the shortcut file on windows.
128+ // If you want to check a file whether is a shortcut file on Windows please use IsShortcut function.
128129func IsSymlink (path string ) bool {
129130 if info , err := os .Lstat (path ); err == nil && info .Mode ()& os .ModeSymlink != 0 {
130131 return true
131132 }
132133 return false
133134}
134135
135- // IsSymlinkE checks a file whether is a symbolic link.
136+ // IsSymlinkE checks a file whether is a symbolic link on Linux .
136137// Note that this doesn't work for the shortcut file on windows.
138+ // If you want to check a file whether is a shortcut file on Windows please use IsShortcut function.
137139func IsSymlinkE (path string ) (bool , error ) {
138140 info , err := os .Lstat (path )
139141 if err == nil && info .Mode ()& os .ModeSymlink != 0 {
@@ -142,27 +144,37 @@ func IsSymlinkE(path string) (bool, error) {
142144 return false , err
143145}
144146
147+ // IsShortcut checks a file whether is a shortcut on Windows.
148+ func IsShortcut (path string ) bool {
149+ ext := filepath .Ext (path )
150+ if ext == ".lnk" {
151+ return true
152+ }
153+ return false
154+ }
155+
145156// RemoveFile removes the named file or empty directory.
146- // https://gist.github.com/novalagung/13c5c8f4d30e0c4bff27
157+ // If there is an error, it will be of type *PathError.
147158func RemoveFile (path string ) error {
148- err := os .Remove (path )
149- return err
159+ return os .Remove (path )
150160}
151161
152162// Create creates or truncates the target file specified by path.
153- // If the parent directory does not exist, it will be created with mode os.ModePerm.is cr truncated.
163+ // If the parent directory does not exist, it will be created with mode os.ModePerm.
154164// If the file does not exist, it is created with mode 0666.
155- // If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.
156- func Create (filePath string ) (* os.File , error ) {
157- if exist , err := IsPathExist (filePath ); err != nil {
165+ // If successful, methods on the returned file can be used for I/O; the associated file descriptor has mode O_RDWR.
166+ func Create (path string ) (* os.File , error ) {
167+ exist , err := IsExist (path )
168+ if err != nil {
158169 return nil , err
159- } else if exist {
160- return os .Create (filePath )
161170 }
162- if err := os .MkdirAll (filepath .Dir (filePath ), os .ModePerm ); err != nil {
171+ if exist {
172+ return os .Create (path )
173+ }
174+ if err := os .MkdirAll (filepath .Dir (path ), os .ModePerm ); err != nil {
163175 return nil , err
164176 }
165- return os .Create (filePath )
177+ return os .Create (path )
166178}
167179
168180// CreateFile creates a file specified by path.
@@ -181,15 +193,16 @@ func FileToBytes(path string) []byte {
181193 return byteStream
182194}
183195
184- // BytesToFile writes data to a file. If the file does not exist it will be created with permission mode 0644.
185- func BytesToFile (filePath string , data []byte ) error {
186- exist , _ := IsPathExist (filePath )
196+ // BytesToFile writes data to a file.
197+ // If the file does not exist it will be created with permission mode 0644.
198+ func BytesToFile (path string , data []byte ) error {
199+ exist , _ := IsExist (path )
187200 if ! exist {
188- if err := CreateFile (filePath ); err != nil {
201+ if err := CreateFile (path ); err != nil {
189202 return err
190203 }
191204 }
192- return ioutil .WriteFile (filePath , data , 0644 )
205+ return ioutil .WriteFile (path , data , 0644 )
193206}
194207
195208// GetDirAllEntryPaths gets all the file or dir paths in the specified directory recursively.
@@ -260,3 +273,13 @@ func GetDirAllEntryPathsFollowSymlink(dirname string, incl bool) ([]string, erro
260273 }
261274 return paths , nil
262275}
276+
277+ // ClearFile clears a file content.
278+ func ClearFile (path string ) error {
279+ f , err := os .OpenFile (path , os .O_WRONLY | os .O_TRUNC , 0777 )
280+ if err != nil {
281+ return err
282+ }
283+ defer f .Close ()
284+ return nil
285+ }
0 commit comments