-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathREADME
More file actions
35 lines (26 loc) · 1.47 KB
/
README
File metadata and controls
35 lines (26 loc) · 1.47 KB
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
tl;dr: strconv.ParseXxx with []byte arguments.
Package bytesconv implements conversions from []byte representations
of basic data types. These are the routines missing from the standard
library's bytesconv. When parsing input data, which naturally arrives as
[]byte, these make parsing the input free of garbage creation. (Using
the bytesconv.ParseXxx functions creates a temporary string which the compiler
isn't (in 1.7) good enough to eliminate. There's a long discussion about
the need for these []byte parsing functions in the go, and the masters
are still, years later, waiting for the compiler to get it right. I can't
wait for my code to get it right, hence this largely C&Ved package.)
Note that only conversions from []byte are implemented. Converting to []byte
is already handled by the standard library's strconv package's AppendXxx
functions.
Numeric Conversions
ParseBool, ParseFloat, ParseInt, and ParseUint convert strings to values:
b, err := bytesconv.ParseBool([]byte("true"))
f, err := bytesconv.ParseFloat([]byte("3.1415"), 64)
i, err := bytesconv.ParseInt([]byte("-42"), 10, 64)
u, err := bytesconv.ParseUint([]byte("42"), 10, 64)
The parse functions return the widest type (float64, int64, and uint64),
but if the size argument specifies a narrower width the result can be
converted to that narrower type without data loss:
s := []byte("2147483647") // biggest int32
i64, err := bytesconv.ParseInt(s, 10, 32)
...
i := int32(i64)