size
dstgo/size string ile dosya boyutu arasında dönüşüm yapmak için kullanışlı bir kütüphanedir.
Depo: dstgo/size: Fast conversion between file size and string (github.com)
Dokümantasyon: dstgo/size: Fast conversion between file size and string (github.com)
Kurulum
$ go get https://github.com/dstgo/sizeKullanım
go
const (
B Size = 1
KB = B * 1024
MB = KB * 1024
GB = MB * 1024
TB = GB * 1024
PB = TB * 1024
)go
type SizeMeta struct {
Data float64
Unit Size
}Bu kütüphane kullanımı çok basittir. Yalnızca iki fonksiyonu kullanıma sunar:
go
func ParseSize(str string) SizeMetaParseSize bir string'i SizeMeta struct'ına dönüştürür. Veri boyutu ve birimini depolar.
go
func ParseTargetSize(str string, size Size) SizeMetaParseTargetSize bir string'i belirtilen boyutta SizeMeta'ya dönüştürür.
Örnek:
go
package main
import (
"fmt"
"github.com/dstgo/size"
)
func main() {
parseSize := size.ParseSize("1.2MB")
fmt.Printf("%+v\n", parseSize)
fmt.Printf("%+v\n", parseSize.Round(1))
parseSize1 := size.ParseSize("2.3-asdajl")
fmt.Printf("%+v\n", parseSize1)
targetSize := size.ParseTargetSize("2.65MB", size.KB)
fmt.Printf("%+v", targetSize)
fmt.Printf("%+v", targetSize.String())
}Dönüşümden sonra ondalık basamak sayısını değiştirmek için Round metodunu veya string temsilini almak için String metodunu kullanabilirsiniz. Örnek çıktı:
1.20MB
1.2
2713.60KB