FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learning-go/src/buffer_reader/nice_buffer_example.go at master · pathbox/learning-go · GitHub
pathbox
/
learning-go
Public
Notifications
You must be signed in to change notification settings
Fork
40
Star
152
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
learning-go
/
src
/
buffer_reader
/
nice_buffer_example.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (56 loc) · 1.54 KB
Breadcrumbs
learning-go
/
src
/
buffer_reader
/
nice_buffer_example.go
Copy path
File metadata and controls
70 lines (56 loc) · 1.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
nicebuf
import
(
"io"
"sync"
"unsafe"
)
type
Buffer
struct
{
Bytes
[]
byte
}
var
_
io.
Writer
=
&
Buffer
{}
// Write a chunk of bytes to the buffer
func
(
b
*
Buffer
)
Write
(
v
[]
byte
) (
int
,
error
) {
b
.
Bytes
=
append
(
b
.
Bytes
,
v
...
)
// nice way
return
len
(
v
),
nil
}
func
(
b
*
Buffer
)
WriteByte
(
v
byte
) {
b
.
Bytes
=
append
(
b
.
Bytes
,
v
)
}
func
(
b
*
Buffer
)
Reset
() {
b
.
Bytes
=
b
.
bytes
[:
0
]
// use this way to reset
}
func
(
b
*
Buffer
)
String
()
string
{
return
*
(
*
string
)(
unsafe
.
Pointer
(
&
b
.
Bytes
))
}
// WriteTo writes the contents of our buffer to an io.Writer
func
(
b
*
Buffer
)
WriteTo
(
w
io.
Writer
) (
int64
,
error
) {
n
,
err
:=
w
.
Write
(
b
.
Bytes
)
return
int64
(
n
),
err
}
// Buffer pool
var
bufpool
=
sync.
Pool
{
New
:
func
()
interface
{} {
return
&
Buffer
{} },
}
// NewBufferFromPool returns a pointer to a zerod Buffer. This may be retrieved from a
// pool. When you're done with it, call 'ReturnToPool'.
func
NewBufferFromPool
()
*
Buffer
{
b
:=
bufpool
.
Get
().(
*
Buffer
)
b
.
Reset
()
return
b
}
// NewBufferFromPoolWithCap returns a pointer to a zero'd Buffer with its underlying
// capacity set. This may be retrieved from a pool. When you're done with it, call 'ReturnToPool'.
func
NewBufferFromPoolWithCap
(
size
int
)
*
Buffer
{
b
:=
bufpool
.
Get
().(
*
Buffer
)
if
c
:=
cap
(
b
.
Bytes
);
c
<
size
{
b
.
Bytes
=
make
([]
byte
,
0
,
size
)
}
else
if
c
>
0
{
b
.
Reset
()
}
return
b
}
// ReturnToPool puts this instance back in the underlying pool. Reading from or using this instance
// in any way after calling this is invalid.
func
(
b
*
Buffer
)
ReturnToPool
() {
bufpool
.
Put
(
b
)
}
Back
|
FazBrowse Home
|
New Git URL