ch12ex08

package
v0.0.0-...-4489971 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ch12ex08 provides an S-expression decoder using an io.Reader instead of a byte slice.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(data []byte, out interface{}) error

Unmarshal parses S-expression data and populates the variable whose address is in the non-nil pointer out.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder reads and decodes S-expressions from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) (err error)

Decode reads the next S-expression from its input and stores it in the value pointed to by v.

Example
b := []byte("8")
var anInt int
if err := Unmarshal(b, &anInt); err != nil {
	log.Fatal("int: " + err.Error())
}
fmt.Printf("%+v\n", anInt)

b = []byte(`"abc"`)
var aStr string
if err := Unmarshal(b, &aStr); err != nil {
	log.Fatal("string: " + err.Error())
}
fmt.Printf("%+v\n", aStr)

b = []byte("(0 1 2)")
var intSlice []int
if err := Unmarshal(b, &intSlice); err != nil {
	log.Fatal("int slice: " + err.Error())
}
fmt.Printf("%+v\n", intSlice)

b = []byte("(3 4 5)")
var intArray [3]int
if err := Unmarshal(b, &intArray); err != nil {
	log.Fatal("int array: " + err.Error())
}
fmt.Printf("%+v\n", intArray)

b = []byte("((M 5) (N 6))")
var aStruct struct{ M, N int }
if err := Unmarshal(b, &aStruct); err != nil {
	log.Fatal("struct: " + err.Error())
}
fmt.Printf("%+v\n", aStruct)

b = []byte(`(("key1" 1) ("key2" 2))`)
var aMap map[string]int
if err := Unmarshal(b, &aMap); err != nil {
	log.Fatal("map: " + err.Error())
}
fmt.Printf("%+v\n", aMap)
Output:

8
abc
[0 1 2]
[3 4 5]
{M:5 N:6}
map[key1:1 key2:2]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL