Golang encoding/hex.InvalidByteError.Error() function example

package encoding/hex

InvalidByteError values describe errors resulting from an invalid byte in a hex string.

Golang encoding/hex.InvalidByteError.Error() function usage example

 package main

 import (
 "encoding/hex"
 "fmt"
 "os"
 )

 func main() {
 str := "abcd你好"  // <----- simulate some error

 b, err := hex.DecodeString(str)

 if err != nil {
 fmt.Println(err.Error())  // <---- here
 os.Exit(1)
 }

 fmt.Printf("Decoded bytes %v \n ", b)
 }

Output :

encoding/hex: invalid byte: U+00E4 'ä'

exit status 1

Reference :

http://golang.org/pkg/encoding/hex/#InvalidByteError.Error

Advertisement