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
+7.9k Golang : Routes multiplexer routing example with regular expression control
+14.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+7.7k Findstr command the Grep equivalent for Windows
+5.7k PHP : Get client IP address
+10.3k Golang : Get local time and equivalent time in different time zone
+17.3k How to enable MariaDB/MySQL logs ?
+18.6k Golang : Check whether a network interface is up on your machine
+20.9k Golang : How to get time zone and load different time zone?
+5.9k Golang : Debug with Godebug
+11.4k Golang : Convert(cast) float to int
+20.8k Golang : Get password from console input without echo or masked