Standard library / encoding/pem

encoding/pem.Decode()

Find the next PEM block and return the remaining input. A nil block indicates that no valid block was found; validate type and payload separately. Expected stdout: 'DEMO Go tail\n'.

Formal Syntax & Structure

go syntaxgo
func Decode(data []byte) (p *Block, rest []byte)

Parameters & Attributes

NameTypeDescription
data[]byteInput bytes that may contain one or more PEM blocks.

Return Value

(p *Block, rest []byte)

Next decoded block (nil if absent), and remaining input bytes.

Working Example

Example Snippetgo
package main
import (  "encoding/pem"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  src := append(pem.EncodeToMemory(&pem.Block{Type: "DEMO", Bytes: []byte("Go")}), []byte("tail")...)  b, rest := pem.Decode(src)  if b == nil {    panic("missing block")  }  fmt.Println(b.Type, string(b.Bytes), string(rest))}
Go Reference