Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
Problem :
How to declare one MegaByte in Golang so that I can use it to calculate filesize allocation?
Solution :
MB is number 2(counting from zero) in the constants order of Golang's builtin byte size enumeration.
type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
EB
ZB
YB
)
Therefore, to declare one mega byte, one would declare it in this way :
1 << (10 * 2)
Below is a full example on how to declare kilobyte up to yottabyte.
package main
import "fmt"
var sizeKB = 1 << (10 * 1) // 1 refers to the constants ByteSize KB
var sizeMB = 5 << (10 * 2) // 2 refers to the constants ByteSize MB -- example of declaring 5 MB
var sizeGB = 1 << (10 * 3) // 3 refers to the constants ByteSize GB
var sizeTB int64 = 1 << (10 * 4) // 4 refers to the constants ByteSize TB -- without int64 will cause overflow error
var sizePB int64 = 1 << (10 * 5) // 5 refers to the constants ByteSize PB
var sizeEB int64 = 1 << (10 * 6) // 6 refers to the constants ByteSize EB
var sizeZB complex128 = 1 << (10 * 7) // 7 refers to the constants ByteSize ZB -- only complex128 can handle this big in Golang
var sizeYB complex128 = 1 << (10 * 8) // 8 refers to the constants ByteSize YB
func main() {
fmt.Printf("Size(raw) : %d Bytes \n", sizeKB)
fmt.Printf("Size(raw) : %d Bytes \n", sizeMB)
fmt.Printf("Size(raw) : %d Bytes \n", sizeGB)
fmt.Printf("Size(raw) : %d Bytes \n", sizeTB)
fmt.Printf("Size(raw) : %d Bytes \n", sizePB)
fmt.Printf("Size(raw) : %d Bytes \n", sizeEB)
fmt.Printf("Size(raw) : %f Bytes \n", sizeZB)
fmt.Printf("Size(raw) : %f Bytes \n", sizeYB)
fmt.Println("--------------------------------------------")
fmt.Printf("Size : %d KiloByte \n", sizeKB/sizeKB)
fmt.Printf("Size : %d MegaByte \n", sizeMB/(1 << (10 * 2))) // example of 5 MB
fmt.Printf("Size : %d GigaByte \n", sizeGB/(1 << (10 * 3)))
fmt.Printf("Size : %d TeraByte \n", sizeTB/(1 << (10 * 4)))
fmt.Printf("Size : %d PetaByte \n", sizePB/(1 << (10 * 5)))
fmt.Printf("Size : %d ExaByte \n", sizeEB/(1 << (10 * 6)))
fmt.Printf("Size : %f ZettaByte \n", sizeZB/(1 << (10 * 7)))
fmt.Printf("Size : %f YottaByte \n", sizeYB/(1 << (10 * 8)))
}
Output :
Size(raw) : 1024 Bytes
Size(raw) : 5242880 Bytes
Size(raw) : 1073741824 Bytes
Size(raw) : 1099511627776 Bytes
Size(raw) : 1125899906842624 Bytes
Size(raw) : 1152921504606846976 Bytes
Size(raw) : (1180591620717411303424.000000+0.000000i) Bytes
Size(raw) : (1208925819614629174706176.000000+0.000000i) Bytes
Size : 1 KiloByte
Size : 5 MegaByte
Size : 1 GigaByte
Size : 1 TeraByte
Size : 1 PetaByte
Size : 1 ExaByte
Size : (1.000000+0.000000i) ZettaByte
Size : (1.000000+0.000000i) YottaByte
References :
See also : Golang : How to split or chunking a file to smaller pieces?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+23k Golang : Print out struct values in string format
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.
+15.7k Golang : Read large file with bufio.Scanner cause token too long error
+8.8k Golang : How to use Gorilla webtoolkit context package properly
+5.5k Swift : Get substring with rangeOfString() function example
+8.1k Golang : Implementing class(object-oriented programming style)
+14k Golang : Convert IP version 6 address to integer or decimal number
+34.8k Golang : Upload and download file to/from AWS S3
+7.9k Golang : Tell color name with OpenCV example
+9.5k PHP : Get coordinates latitude/longitude from string
+5.5k List of Golang XML tutorials
+8.8k Golang : automatically figure out array length(size) with three dots