10 examples of 'golang file size' in Go

Every line of 'golang file size' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
94func FileSize(file string) (int64, error) {
95 f, e := os.Stat(file)
96 if e != nil {
97 return 0, e
98 }
99 return f.Size(), nil
100}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

210func FileSize(path string) (int64, error) {
211 fi, err := os.Stat(path)
212 if err != nil {
213 return 0, err
214 }
215 return fi.Size(), nil
216}
115func (m *Request) GetFilesize() int64 {
116 if m != nil {
117 return m.Filesize
118 }
119 return 0
120}
81func GetFilesSizeMB(files []string) (int, error) {
82 var size int64
83
84 for _, f := range files {
85 file, err := os.Open(f)
86 if err != nil {
87 return 0, err
88 }
89 fi, err := file.Stat()
90 if err != nil {
91 return 0, err
92 }
93
94 size += fi.Size()
95 file.Close()
96 }
97
98 return int(size / 1048576), nil
99}
13func FileSize(sizeBytes int) string {
14 if sizeBytes >= 1<<30 {
15 return fmt.Sprintf("%dgB", sizeBytes/(1<<30))
16 } else if sizeBytes >= 1<<20 {
17 return fmt.Sprintf("%dmB", sizeBytes/(1<<20))
18 } else if sizeBytes >= 1<<10 {
19 return fmt.Sprintf("%dkB", sizeBytes/(1<<10))
20 }
21 return fmt.Sprintf("%dB", sizeBytes)
22}
94func (r *BasicReader) FileSize() (int64, error) {
95 info, err := r.file.Stat()
96 if err != nil {
97 return 0, err
98 }
99 return info.Size(), nil
100}
148func (dr *PBDagReader) Size() uint64 {
149 return dr.pbdata.GetFilesize()
150}
13func FileSize(size int64) string {
14 s := float64(size)
15 if s > 1024*1024 {
16 return fmt.Sprintf("%.1f M", s/(1024*1024))
17 }
18 if s > 1024 {
19 return fmt.Sprintf("%.1f K", s/1024)
20 }
21 return fmt.Sprintf("%f B", s)
22}
145func FileSize(path string) (int64, error) {
146 var size int64 = -1
147 if FileExists(path) {
148 info, err := os.Stat(path)
149 if err != nil {
150 if err != nil {
151 return size, fmt.Errorf("unable to obtain information about file: %s\n%s", path, err)
152 }
153 return size, err
154 }
155 size = info.Size()
156 } else {
157 return size, fmt.Errorf("file does not exist")
158 }
159 return size, nil
160}
97func fileSize(f *os.File) (int64, error) {
98 fi, e := f.Stat()
99 if e != nil {
100 return 0, e
101 }
102 return fi.Size(), nil
103}

Related snippets