-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file_size.go
More file actions
40 lines (30 loc) · 825 Bytes
/
read_file_size.go
File metadata and controls
40 lines (30 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package go_lib
import (
"log"
"os"
)
/*
Read and return the file size of a file located at a provided file path.
Negative return values are errors.
-1 is an inability to open a file for reading.
-2 is an inability to call os.Stat() on a file.
*/
func ReadFileSize(filepath string) int {
/* Read in file's size */
/* Open a file for read access */
if file, err := os.Open(filepath); err != nil {
log.Println(err)
return -1
} else {
/* Defer file closing */
defer file.Close()
/* Call os.Stat() on file in order to retrieve file size. */
if file_stats, err := file.Stat(); err != nil {
log.Println(err)
return -2
} else {
/* Allow defer to handle file closing and return file size to calling function. */
return int(file_stats.Size())
}
}
}