Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is func Decode(dst, src []byte, flush bool) in Golang?

Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err error) is a method defined in the encoding/ascii85 package in Golang that decodes ascii-85 encoded source bytes of an array to a destination bytes array.

Decode() returns the number of bytes successfully written in the destination array, the number of bytes consumed from the source array, and a CorruptInputError.

Parameters

src: An ascii-85 encoded source byte array that will be decoded.

dst: Destination byte to the array to which the decoded output will be written.

flush: When true, Decode() processes the src array completely. When false, it waits for the completion of another 32-bit block.

Return value

ndst: Number of bytes written to dst. Type is int.

nsrc: Number of bytes consumed from src. Type is int.

Example

package main
import (
"encoding/ascii85"
"fmt"
)
func main() {
src1 := []byte("Ascii85 encoding in Golang.")
dst1 := make([]byte, ascii85.MaxEncodedLen(len(src1)))
var bytes_written int = ascii85.Encode(dst1, src1)
fmt.Printf("Encoded array: %s\n", dst1)
fmt.Printf("Number of bytes written: %d\n", bytes_written)
dst2 := make([]byte, ascii85.MaxEncodedLen(len(dst1)))
ndst, nsrc, _ := ascii85.Decode(dst2, dst1, true)
fmt.Printf("Decoded array: %s\n", dst2)
fmt.Printf("Number of bytes written: %d\n", ndst)
fmt.Printf("Number of bytes consumed: %d\n", nsrc)
}

Explanation

In the above example, we first do an ascii-85 encoding of src1 containing the string “Ascii85 encoding in Golang.” using ascii85.Encode() and store the result in dst1.

We then use the ascii85.Decode() method that decodes dst1 and stores the result in dst2. As you can observe, the Decode() method returns the original string.

RELATED TAGS

golang

CONTRIBUTOR

Abdul Monum
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring