-
Notifications
You must be signed in to change notification settings - Fork 11
/
defs.template
45 lines (34 loc) · 925 Bytes
/
defs.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gonpy
import (
"fmt"
"encoding/binary"
)
{{- range . }}
// Get{{ .TypeU }} returns the array data as a slice of {{ .TypeL }} values.
func (rdr *NpyReader) Get{{ .TypeU }}() ([]{{ .TypeL }}, error) {
if rdr.Dtype != "{{ .TypeCode }}" {
return nil, fmt.Errorf("Reader does not contain {{ .TypeL }} data")
}
data := make([]{{ .TypeL }}, rdr.nElt)
err := binary.Read(rdr.r, rdr.Endian, &data)
if err != nil {
return nil, err
}
return data, nil
}
{{ end }}
{{- range . }}
// Write{{ .TypeU }} writes a slice of {{ .TypeL }} values in npy format.
func (wtr *NpyWriter) Write{{ .TypeU }}(data []{{ .TypeL }}) error {
err := wtr.writeHeader("{{ .TypeCode }}", len(data))
if err != nil {
return err
}
err = binary.Write(wtr.w, wtr.Endian, data)
if err != nil {
return err
}
wtr.w.Close()
return nil
}
{{ end }}