-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
70 lines (66 loc) · 1.77 KB
/
buffer.go
File metadata and controls
70 lines (66 loc) · 1.77 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package converter
import "bytes"
// TODO: Adicionar exemplos
// ToBuffer attempts to convert a given value into a buffer. It internally uses ToBufferWithErr function that
// converts the given value to a byte slice first.
// If the conversion fails, it panics and stops the execution.
//
// Parameters:
// - a: The value of any type to be converted to a buffer.
//
// Returns:
// - *bytes.Buffer: The buffer representation of the passed value.
//
// Example:
//
// myValue := "This is test"
// buffer := ToBuffer(myValue)
// fmt.Println(buffer) // This is test
//
// myValue := 1234
// buffer := ToBuffer(myValue)
// fmt.Println(buffer) // 1234
//
// Panics:
// - If the conversion to a buffer fails at any point, it will panic.
func ToBuffer(a any) *bytes.Buffer {
buf, err := ToBufferWithErr(a)
if err != nil {
panic(err)
}
return buf
}
// ToBufferWithErr attempts to convert a given value to a bytes.Buffer
// It first tries to convert the supplied argument to a byte slice by
// calling the function ToBytesWithErr. If a conversion error occurs,
// it returns nil along with the error.
//
// Parameters:
// - a: The value of any type to be converted to a bytes.Buffer
//
// Returns:
// - *bytes.Buffer: The bytes.Buffer representation of the provided value.
// - error: An error is returned in case of failure to convert.
//
// Example:
//
// myValue := "This is a test"
// buffer, err := ToBufferWithErr(myValue)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(buffer) // This is a test
//
// num := 1234
// buffer, err := ToBufferWithErr(num)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(buffer) // 1234
func ToBufferWithErr(a any) (*bytes.Buffer, error) {
bs, err := ToBytesWithErr(a)
if err != nil {
return nil, err
}
return bytes.NewBuffer(bs), nil
}