### Accessing Byte Unit Constants Source: https://context7.com/cloudfoundry/bytefmt/llms.txt Demonstrates the use of exported constants for byte unit sizes, which are based on binary calculations (1024 bytes per unit). ```go package main import ( "fmt" "code.cloudfoundry.org/bytefmt" ) func main() { // Available constants (all in bytes) fmt.Println("BYTE:", bytefmt.BYTE) // 1 fmt.Println("KILOBYTE:", bytefmt.KILOBYTE) // 1024 fmt.Println("MEGABYTE:", bytefmt.MEGABYTE) // 1048576 fmt.Println("GIGABYTE:", bytefmt.GIGABYTE) // 1073741824 fmt.Println("TERABYTE:", bytefmt.TERABYTE) // 1099511627776 fmt.Println("PETABYTE:", bytefmt.PETABYTE) // 1125899906842624 fmt.Println("EXABYTE:", bytefmt.EXABYTE) // 1152921504606846976 // Use constants for calculations memoryLimit := 4 * bytefmt.GIGABYTE fmt.Printf("Memory limit: %d bytes\n", memoryLimit) // Output: Memory limit: 4294967296 bytes } ``` -------------------------------- ### Formatting Bytes to Human-Readable Strings Source: https://context7.com/cloudfoundry/bytefmt/llms.txt Converts uint64 byte counts into human-readable strings using the largest appropriate unit with short notation. ```go package main import ( "fmt" "code.cloudfoundry.org/bytefmt" ) func main() { // Basic formatting - automatically selects appropriate unit fmt.Println(bytefmt.ByteSize(0)) // Output: 0B fmt.Println(bytefmt.ByteSize(1)) // Output: 1B fmt.Println(bytefmt.ByteSize(1024)) // Output: 1K fmt.Println(bytefmt.ByteSize(1536)) // Output: 1.5K fmt.Println(bytefmt.ByteSize(1048576)) // Output: 1M fmt.Println(bytefmt.ByteSize(1073741824)) // Output: 1G fmt.Println(bytefmt.ByteSize(1099511627776)) // Output: 1T fmt.Println(bytefmt.ByteSize(1125899906842624)) // Output: 1P fmt.Println(bytefmt.ByteSize(1152921504606846976)) // Output: 1E // Using constants for clarity fmt.Println(bytefmt.ByteSize(100 * bytefmt.MEGABYTE)) // Output: 100M fmt.Println(bytefmt.ByteSize(uint64(100.5 * bytefmt.MEGABYTE))) // Output: 100.5M fmt.Println(bytefmt.ByteSize(10 * bytefmt.GIGABYTE)) // Output: 10G fmt.Println(bytefmt.ByteSize(uint64(10.5 * bytefmt.GIGABYTE))) // Output: 10.5G // Practical example: display file sizes files := map[string]uint64{ "small.txt": 512, "medium.doc": 2 * bytefmt.MEGABYTE, "large.zip": uint64(1.5 * bytefmt.GIGABYTE), "huge.iso": 4 * bytefmt.GIGABYTE, } for name, size := range files { fmt.Printf("%s: %s\n", name, bytefmt.ByteSize(size)) } // Output: // small.txt: 512B // medium.doc: 2M // large.zip: 1.5G // huge.iso: 4G } ``` -------------------------------- ### Parse Human-Readable String to Bytes Source: https://context7.com/cloudfoundry/bytefmt/llms.txt Converts strings representing byte quantities into their integer byte equivalent. Supports short, long, and binary-prefixed units, and returns an error for invalid or negative inputs. ```go package main import ( "fmt" "log" "code.cloudfoundry.org/bytefmt" ) func main() { // Short unit format (K, M, G, T, P, E) bytes, err := bytefmt.ToBytes("5M") if err != nil { log.Fatal(err) } fmt.Printf("5M = %d bytes\n", bytes) // Output: 5M = 5242880 bytes // Long unit format (KB, MB, GB, TB, PB, EB) bytes, _ = bytefmt.ToBytes("2GB") fmt.Printf("2GB = %d bytes\n", bytes) // Output: 2GB = 2147483648 bytes // Binary-prefixed format (KiB, MiB, GiB, TiB, PiB, EiB) bytes, _ = bytefmt.ToBytes("512MiB") fmt.Printf("512MiB = %d bytes\n", bytes) // Output: 512MiB = 536870912 bytes // Case-insensitive bytes, _ = bytefmt.ToBytes("1gb") fmt.Printf("1gb = %d bytes\n", bytes) // Output: 1gb = 1073741824 bytes // Handles whitespace bytes, _ = bytefmt.ToBytes(" 100K ") fmt.Printf("' 100K ' = %d bytes\n", bytes) // Output: ' 100K ' = 102400 bytes // Decimal values supported bytes, _ = bytefmt.ToBytes("13.5KB") fmt.Printf("13.5KB = %d bytes\n", bytes) // Output: 13.5KB = 13824 bytes bytes, _ = bytefmt.ToBytes("4.5KB") fmt.Printf("4.5KB = %d bytes\n", bytes) // Output: 4.5KB = 4608 bytes // Error handling examples _, err = bytefmt.ToBytes("5") // Missing unit fmt.Printf("Error for '5': %v\n", err) // Output: Error for '5': byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB _, err = bytefmt.ToBytes("-5MB") // Negative value fmt.Printf("Error for '-5MB': %v\n", err) // Output: Error for '-5MB': byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB _, err = bytefmt.ToBytes("5MBB") // Invalid unit fmt.Printf("Error for '5MBB': %v\n", err) // Output: Error for '5MBB': byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB // Practical example: parse configuration value configValue := "256M" memLimit, err := bytefmt.ToBytes(configValue) if err != nil { log.Fatalf("Invalid memory configuration: %v", err) } fmt.Printf("Memory limit set to %d bytes\n", memLimit) // Output: Memory limit set to 268435456 bytes } ``` -------------------------------- ### Parse Human-Readable String to Megabytes Source: https://context7.com/cloudfoundry/bytefmt/llms.txt Converts strings representing byte quantities into their integer megabyte equivalent, rounding down. Useful for system memory or disk specifications. ```go package main import ( "fmt" "log" "code.cloudfoundry.org/bytefmt" ) func main() { // Parse various units to megabytes mb, err := bytefmt.ToMegabytes("5M") if err != nil { log.Fatal(err) } fmt.Printf("5M = %d MB\n", mb) // Output: 5M = 5 MB mb, _ = bytefmt.ToMegabytes("2G") fmt.Printf("2G = %d MB\n", mb) // Output: 2G = 2048 MB mb, _ = bytefmt.ToMegabytes("3T") fmt.Printf("3T = %d MB\n", mb) // Output: 3T = 3145728 MB mb, _ = bytefmt.ToMegabytes("4P") fmt.Printf("4P = %d MB\n", mb) // Output: 4P = 4294967296 MB mb, _ = bytefmt.ToMegabytes("5E") fmt.Printf("5E = %d MB\n", mb) // Output: 5E = 5497558138880 MB // Small values round down to 0 mb, _ = bytefmt.ToMegabytes("5B") fmt.Printf("5B = %d MB\n", mb) // Output: 5B = 0 MB mb, _ = bytefmt.ToMegabytes("5K") fmt.Printf("5K = %d MB\n", mb) // Output: 5K = 0 MB // All unit formats supported mb, _ = bytefmt.ToMegabytes("5MB") fmt.Printf("5MB = %d MB\n", mb) // Output: 5MB = 5 MB mb, _ = bytefmt.ToMegabytes("5MiB") fmt.Printf("5MiB = %d MB\n", mb) // Output: 5MiB = 5 MB // Case-insensitive mb, _ = bytefmt.ToMegabytes("5mb") fmt.Printf("5mb = %d MB\n", mb) // Output: 5mb = 5 MB mb, _ = bytefmt.ToMegabytes("5mib") fmt.Printf("5mib = %d MB\n", mb) // Output: 5mib = 5 MB // Practical example: Cloud Foundry app memory configuration appMemory := "512M" memMB, err := bytefmt.ToMegabytes(appMemory) if err != nil { log.Fatalf("Invalid memory setting: %v", err) } fmt.Printf("App configured with %d MB of memory\n", memMB) // Output: App configured with 512 MB of memory // Error handling (same as ToBytes) _, err = bytefmt.ToMegabytes("invalid") if err != nil { fmt.Printf("Parse error: %v\n", err) } // Output: Parse error: byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB } ``` -------------------------------- ### ToMegabytes - Parse Human-Readable String to Megabytes Source: https://context7.com/cloudfoundry/bytefmt/llms.txt The ToMegabytes function parses a human-readable byte string and returns the equivalent value in megabytes, rounded down. It accepts the same unit formats as ToBytes and is useful for specifying memory or disk sizes in megabytes. ```APIDOC ## ToMegabytes - Parse Human-Readable String to Megabytes ### Description Parses a human-readable byte string and returns the equivalent value in megabytes (rounded down). Accepts the same unit formats as ToBytes. Useful for systems requiring memory or disk specifications in megabytes. ### Method N/A (This is a function, not an API endpoint) ### Endpoint N/A ### Parameters N/A (Function arguments are passed directly) ### Request Example ```go package main import ( "fmt" "log" "code.cloudfoundry.org/bytefmt" ) func main() { mb, err := bytefmt.ToMegabytes("5M") if err != nil { log.Fatal(err) } fmt.Printf("5M = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("2G") fmt.Printf("2G = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("3T") fmt.Printf("3T = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("512M") fmt.Printf("512M = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("5MB") fmt.Printf("5MB = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("5MiB") fmt.Printf("5MiB = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("5mb") fmt.Printf("5mb = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("5mib") fmt.Printf("5mib = %d MB\n", mb) mb, _ = bytefmt.ToMegabytes("5B") fmt.Printf("5B = %d MB\n", mb) } ``` ### Response #### Success Response - **mb** (int64) - The equivalent value in megabytes, rounded down. #### Response Example ```json { "mb": 5 } ``` #### Error Response - **error** (error) - An error object if parsing fails. #### Error Example ```json { "error": "byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB" } ``` ``` -------------------------------- ### ToBytes - Parse Human-Readable String to Bytes Source: https://context7.com/cloudfoundry/bytefmt/llms.txt The ToBytes function parses a human-readable byte string into its equivalent value in bytes. It supports short (K, M, G), long (KB, MB, GB), and binary-prefixed (KiB, MiB, GiB) units. The function is case-insensitive, trims whitespace, and returns an error for invalid formats, missing units, or negative values. ```APIDOC ## ToBytes - Parse Human-Readable String to Bytes ### Description Parses a human-readable byte string into its equivalent value in bytes. Supports short (K, M, G), long (KB, MB, GB), and binary-prefixed (KiB, MiB, GiB) units. Case-insensitive and trims whitespace. Returns an error for invalid formats, missing units, or negative values. ### Method N/A (This is a function, not an API endpoint) ### Endpoint N/A ### Parameters N/A (Function arguments are passed directly) ### Request Example ```go package main import ( "fmt" "log" "code.cloudfoundry.org/bytefmt" ) func main() { bytes, err := bytefmt.ToBytes("5M") if err != nil { log.Fatal(err) } fmt.Printf("5M = %d bytes\n", bytes) bytes, _ = bytefmt.ToBytes("2GB") fmt.Printf("2GB = %d bytes\n", bytes) bytes, _ = bytefmt.ToBytes("512MiB") fmt.Printf("512MiB = %d bytes\n", bytes) bytes, _ = bytefmt.ToBytes("1gb") fmt.Printf("1gb = %d bytes\n", bytes) bytes, _ = bytefmt.ToBytes(" 100K ") fmt.Printf("' 100K ' = %d bytes\n", bytes) bytes, _ = bytefmt.ToBytes("13.5KB") fmt.Printf("13.5KB = %d bytes\n", bytes) } ``` ### Response #### Success Response - **bytes** (int64) - The equivalent value in bytes. #### Response Example ```json { "bytes": 5242880 } ``` #### Error Response - **error** (error) - An error object if parsing fails. #### Error Example ```json { "error": "byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB" } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.