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
+28.8k Golang : Get first few and last few characters from string
+14.4k Golang : Execute function at intervals or after some delay
+7k Golang : How to iterate a slice without using for loop?
+12.3k Golang : Exit, terminating or aborting a program
+33.7k Golang : Proper way to set function argument default value
+7.9k Golang : Variadic function arguments sanity check example
+32.4k Golang : Regular Expression for alphanumeric and underscore
+13.8k Golang : concatenate(combine) strings
+15.3k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+8.4k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+13.8k Javascript : Prompt confirmation before exit
+11.4k Golang : Concurrency and goroutine example