replace zxq.co/ripple/hanayo
This commit is contained in:
29
vendor/github.com/valyala/fasthttp/bytebuffer_example_test.go
generated
vendored
Normal file
29
vendor/github.com/valyala/fasthttp/bytebuffer_example_test.go
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package fasthttp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func ExampleByteBuffer() {
|
||||
// This request handler sets 'Your-IP' response header
|
||||
// to 'Your IP is <ip>'. It uses ByteBuffer for constructing response
|
||||
// header value with zero memory allocations.
|
||||
yourIPRequestHandler := func(ctx *fasthttp.RequestCtx) {
|
||||
b := fasthttp.AcquireByteBuffer()
|
||||
b.B = append(b.B, "Your IP is <"...)
|
||||
b.B = fasthttp.AppendIPv4(b.B, ctx.RemoteIP())
|
||||
b.B = append(b.B, ">"...)
|
||||
ctx.Response.Header.SetBytesV("Your-IP", b.B)
|
||||
|
||||
fmt.Fprintf(ctx, "Check response headers - they must contain 'Your-IP: %s'", b.B)
|
||||
|
||||
// It is safe to release byte buffer now, since it is
|
||||
// no longer used.
|
||||
fasthttp.ReleaseByteBuffer(b)
|
||||
}
|
||||
|
||||
// Start fasthttp server returning your ip in response headers.
|
||||
fasthttp.ListenAndServe(":8080", yourIPRequestHandler)
|
||||
}
|
Reference in New Issue
Block a user