From 4838a1852ed5f908d005ddb350329439b3be4e44 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 9 Dec 2015 13:54:41 +0200 Subject: [PATCH] Updated tips and tricks section in README --- README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ba33e6..e25bdd1 100644 --- a/README.md +++ b/README.md @@ -368,10 +368,15 @@ var ( dst []byte src []byte ) -dst = append(dst, src...) // this is legal code -copy(dst, src) // this is legal code -(string(src) == "") // is true -(len(src) == 0) // is true +dst = append(dst, src...) // is legal if dst is nil and src is nil +copy(dst, src) // is legal if dst is nil and src is nil +(string(src) == "") // is true if src is nil +(len(src) == 0) // is true if src is nil + +// this for loop doesn't panic if src is nil +for i, ch := range src { + doSomething(i, ch) +} ``` So throw away nil checks for `[]byte` buffers from you code. For example, @@ -393,6 +398,13 @@ srcLen := len(src) dst = append(dst, "foobar"...) ``` +* `[]byte` buffer may be extended to its' capacity. +```go +buf := make([]byte, 100) +a := buf[:10] // len(a) == 10, cap(a) == 100. +b := a[:100] // is valid, since cap(a) == 100. +``` + * All fasthttp functions accept nil `[]byte` buffer ```go statusCode, body, err := fasthttp.Get(nil, "http://google.com/")