How to define an exabyte constant in Go

1 Answer

0 votes
package main

import "fmt"

/*
   Go's constants are evaluated at compile time and can be arbitrarily large
   until they are given a concrete type.

   Exabyte sizes:

       1 EiB = 2^60  = 1,152,921,504,606,846,976 bytes
       1 EB  = 10^18 = 1,000,000,000,000,000,000 bytes

   Using iota with bit shifting is the idiomatic Go way to define binary units.
*/

const (
    // Binary exabyte (exbibyte)
    // 1 << (10 * 6) = 2^60 bytes
    ExabyteEiB = 1 << (10 * 6)

    // Decimal exabyte (SI), using underscores for readability
    ExabyteEB = 1_000_000_000_000_000_000
)

func main() {
    fmt.Println("1 EiB =", ExabyteEiB, "bytes")
    fmt.Println("1 EB  =", ExabyteEB,  "bytes")
}


/*
run:

1 EiB = 1152921504606846976 bytes
1 EB  = 1000000000000000000 bytes

*/

 



answered May 3 by avibootz
...