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
+10.4k Golang : Get UDP client IP address and differentiate clients by port number
+33.5k Golang : Proper way to set function argument default value
+19k Golang : Example for DSA(Digital Signature Algorithm) package functions
+10.2k Golang : Flip coin example
+12.9k Golang : error parsing regexp: invalid or unsupported Perl syntax
+8.3k Golang : Set or add headers for many or different handlers
+6.2k Unix/Linux : How to get own IP address ?
+12.9k Facebook PHP getUser() returns 0
+6.5k Golang : Pat multiplexer routing example
+12k Elastic Search : Return all records (higher than default 10)
+9.1k Golang : Changing a RGBA image number of channels with OpenCV
+6.3k Golang : Humanize and Titleize functions