How to use the MaxEncodedLen function in Go

The MaxEncodedLen() function in Go reports the maximum encoding length of a given number of bytes.

The program needs to include the encoding/ascii85 module to use the MaxEncodedLen() function.

Prototype

Parameters and return type

The MaxEncodedLen() function accepts the number of source bytes as type int and returns the maximum encoded length as type int.

Example

The following code demonstrates how to use the MaxEncodedLen() function:

package main
import (
"fmt"
"encoding/ascii85"
)
func main() {
// calculate encoding len of a source of length 5
x := ascii85.MaxEncodedLen(5)
// display maximum encoded length
fmt.Println(x)
}

The above program takes the length of a byte source, which is 5, and calculates its maximum encoded length, which is 10.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved