@@ -108,69 +108,75 @@ func main() {
108108package main
109109
110110import (
111- " context "
112- " encoding/gob "
113- " bytes "
114- " fmt"
111+ " bytes "
112+ " context "
113+ " encoding/gob "
114+ " fmt"
115115
116- " github.com/lanrat/extsort"
116+ " github.com/lanrat/extsort"
117117)
118118
119119type Person struct {
120- Name string
121- Age int
120+ Name string
121+ Age int
122122}
123123
124124func personToBytes (p Person ) []byte {
125- var buf bytes.Buffer
126- enc := gob.NewEncoder (&buf)
127- enc.Encode (p)
128- return buf.Bytes ()
125+ var buf bytes.Buffer
126+ enc := gob.NewEncoder (&buf)
127+ err := enc.Encode (p)
128+ if err != nil {
129+ panic (err)
130+ }
131+ return buf.Bytes ()
129132}
130133
131134func personFromBytes (data []byte ) Person {
132- var p Person
133- buf := bytes.NewReader (data)
134- dec := gob.NewDecoder (buf)
135- dec.Decode (&p)
136- return p
135+ var p Person
136+ buf := bytes.NewReader (data)
137+ dec := gob.NewDecoder (buf)
138+ err := dec.Decode (&p)
139+ if err != nil {
140+ panic (err)
141+ }
142+ return p
137143}
138144
139145func comparePersons (a , b Person ) bool {
140- return a.Age < b.Age // Sort by age
146+ return a.Age < b.Age // Sort by age
141147}
142148
143149func main () {
144- people := []Person{
145- {" Alice" , 30 },
146- {" Bob" , 25 },
147- {" Charlie" , 35 },
148- }
149-
150- inputChan := make (chan Person, len (people))
151- for _ , person := range people {
152- inputChan <- person
153- }
154- close (inputChan)
155-
156- sorter , outputChan , errChan := extsort.Generic (
157- inputChan,
158- personFromBytes,
159- personToBytes,
160- comparePersons,
161- nil ,
162- )
163-
164- go sorter.Sort (context.Background ())
165-
166- fmt.Println (" People sorted by age:" )
167- for person := range outputChan {
168- fmt.Printf (" %s (age %d )\n " , person.Name , person.Age )
169- }
170-
171- if err := <- errChan; err != nil {
172- panic (err)
173- }
150+ people := []Person{
151+ {" Alice" , 30 },
152+ {" Bob" , 25 },
153+ {" Charlie" , 35 },
154+ }
155+
156+ inputChan := make (chan Person, len (people))
157+ for _ , person := range people {
158+ inputChan <- person
159+ }
160+ close (inputChan)
161+
162+ sorter , outputChan , errChan := extsort.Generic (
163+ inputChan,
164+ personFromBytes,
165+ personToBytes,
166+ comparePersons,
167+ nil ,
168+ )
169+
170+ go sorter.Sort (context.Background ())
171+
172+ fmt.Println (" People sorted by age:" )
173+ for person := range outputChan {
174+ fmt.Printf (" %s (age %d )\n " , person.Name , person.Age )
175+ }
176+
177+ if err := <- errChan; err != nil {
178+ panic (err)
179+ }
174180}
175181```
176182
0 commit comments