From 3bd5fad4c0a972dd279ecfbf77de31838764f609 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 22 Mar 2023 13:54:25 +0700 Subject: lib/bytes: add function SplitEach The SplitEach funciton split the slice of byte into n number of bytes. If n is less or equal than zero, it will return the data as chunks. --- lib/bytes/bytes_example_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/bytes/bytes_example_test.go') diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go index 314275d2..da8dcce2 100644 --- a/lib/bytes/bytes_example_test.go +++ b/lib/bytes/bytes_example_test.go @@ -351,6 +351,22 @@ func ExampleSnippetByIndexes() { // reserved. } +func ExampleSplitEach() { + var data = []byte(`Hello`) + + fmt.Printf("%s\n", SplitEach(data, 0)) + fmt.Printf("%s\n", SplitEach(data, 1)) + fmt.Printf("%s\n", SplitEach(data, 2)) + fmt.Printf("%s\n", SplitEach(data, 5)) + fmt.Printf("%s\n", SplitEach(data, 10)) + // Output: + // [Hello] + // [H e l l o] + // [He ll o] + // [Hello] + // [Hello] +} + func ExampleTokenFind() { text := []byte("// Copyright 2018, Shulhan . All rights reserved.") -- cgit v1.3-6-g1900