func decodeString4(r io.Reader) ([]byte, error) {
	l := make([]byte, 4)
	_, err := r.Read(l)
	if err != nil {
		return []byte{}, err
	}
	length := int(binary.BigEndian.Uint32(l))
	data := make([]byte, length)
	n, err := r.Read(data)
	if err != nil && err != io.EOF {
		return []byte{}, err
	}
	if n < length {
		return []byte{}, fmt.Errorf("truncated data")
	}
	return data, nil
}
Decode a string with length on 32 bits.
package bertrpc  
import (
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"reflect"
)
var ErrRange = errors.New("value out of range")
func Decode(r io.Reader, term interface{}) error {
	byte1 := make([]byte, 1)
	_, err := r.Read(byte1)
	if err != nil {
		return err
	}
	if byte1[0] != byte(TagETFVersion) {
		return fmt.Errorf("incorrect Erlang Term version tag: %d", byte1[0])
	}
	return decodeData(r, term)
}
func decodeData(r io.Reader, term interface{}) error {
	val := reflect.ValueOf(term)
	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}
	switch val.Kind() {
	case reflect.Int8:
		return ErrRange
	case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
		i, err := decodeInt(r)
		if err == nil {
			val.SetInt(i)
		}
		return err
	case reflect.String:
		s, err := decodeString(r)
		if err == nil {
			val.SetString(s)
		}
		return err
	case reflect.Struct:
		if val.Type().Name() == "String" {
			return decodeBertString(r, val)
		}
		return decodeStruct(r, val)
	default:
		return fmt.Errorf("unhandled decoding target: %s", val.Kind())
	}
}
func decodeInt(r io.Reader) (int64, error) {
	byte1 := make([]byte, 1)
	_, err := r.Read(byte1)
	if err != nil {
		return 0, err
	}
	switch int(byte1[0]) {
	case TagSmallInteger:
		_, err = r.Read(byte1)
		if err != nil {
			return 0, err
		}
		return int64(byte1[0]), nil
	case TagInteger:
		byte4 := make([]byte, 4)
		n, err := r.Read(byte4)
		if err != nil {
			return 0, err
		}
		if n < 4 {
			return 0, fmt.Errorf("cannot decode integer, only %d bytes read", n)
		}
		var32 := int32(binary.BigEndian.Uint32(byte4))
		return int64(var32), nil
	}
	return 0, fmt.Errorf("incorrect type")
}
func decodeString(r io.Reader) (string, error) {
	byte1 := make([]byte, 1)
	_, err := r.Read(byte1)
	if err != nil {
		return "", err
	}
	dataType := int(byte1[0])
	switch dataType {
	case TagSmallAtomUTF8:
		data, err := decodeString1(r)
		return string(data), err
	case TagDeprecatedAtom, TagAtomUTF8, TagString:
		data, err := decodeString2(r)
		return string(data), err
	case TagBinary:
		data, err := decodeString4(r)
		return string(data), err
	case TagList:
		data, err := decodeCharList(r)
		return string(data), err
	}
	return "", fmt.Errorf("incorrect type: %d", dataType)
}
func decodeString1(r io.Reader) ([]byte, error) {
	byte1 := make([]byte, 1)
	_, err := r.Read(byte1)
	if err != nil {
		return []byte{}, err
	}
	length := int(byte1[0])
	data := make([]byte, length)
	n, err := r.Read(data)
	if err != nil && err != io.EOF {
		return []byte{}, err
	}
	if n < length {
		return []byte{}, fmt.Errorf("truncated data")
	}
	return data, nil
}
func decodeString2(r io.Reader) ([]byte, error) {
	l := make([]byte, 2)
	_, err := r.Read(l)
	if err != nil {
		return []byte{}, err
	}
	length := int(binary.BigEndian.Uint16(l))
	data := make([]byte, length)
	n, err := r.Read(data)
	if err != nil && err != io.EOF {
		return []byte{}, err
	}
	if n < length {
		return []byte{}, fmt.Errorf("truncated data")
	}
	return data, nil
}