How to define a terabyte 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 assigned to a typed variable.

   Terabyte sizes:

       1 TiB = 2^40  = 1,099,511,627,776 bytes
       1 TB  = 10^12 = 1,000,000,000,000 bytes

   Using bit shifting for binary units is idiomatic Go.
*/

const (
    // Binary terabyte (tebibyte)
    // 1 << 40 = 2^40 bytes
    TerabyteTiB = 1 << 40

    // Decimal terabyte (SI), using underscores for readability
    TerabyteTB = 1_000_000_000_000
)

func main() {
    fmt.Println("1 TiB =", TerabyteTiB, "bytes")
    fmt.Println("1 TB  =", TerabyteTB,  "bytes")
}



/*
run:

1 TiB = 1099511627776 bytes
1 TB  = 1000000000000 bytes

*/

 



answered May 3 by avibootz
...