728x90
Go 파일 사이즈 구하기
파일사이즈 구하는 소스
package main
import (
"os"
"strings"
"fmt"
"log"
)
const (
BYTE = 1.0
KILOBYTE = 1024 * BYTE
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
TERABYTE = 1024 * GIGABYTE
)
func ByteSize(bytes uint64) string {
unit := ""
value := float32(bytes)
switch {
case bytes >= TERABYTE:
unit = "T"
value = value / TERABYTE
case bytes >= GIGABYTE:
unit = "G"
value = value / GIGABYTE
case bytes >= MEGABYTE:
unit = "M"
value = value / MEGABYTE
case bytes >= KILOBYTE:
unit = "K"
value = value / KILOBYTE
case bytes >= BYTE:
unit = "B"
case bytes == 0:
return "0"
}
stringValue := fmt.Sprintf("%.1f", value)
stringValue = strings.TrimSuffix(stringValue, ".0")
return fmt.Sprintf("%s%s", stringValue, unit)
}
func main() {
stat, err := os.Stat(filePath)
if err != nil {
log.Fatal(err)
}
fmt.Println(ByteSize(uint64(stat.Size()))
}
728x90
'Go' 카테고리의 다른 글
Go URL로 파일 다운로드 (0) | 2023.02.14 |
---|---|
Go 파일 읽기 (0) | 2022.11.24 |
Go AWS S3 GetObject 파일 가져오기 (0) | 2022.09.30 |
Go 랜덤 문자열 만들기 (0) | 2022.09.21 |
Go 수행시간 측정 (0) | 2017.09.26 |