ADVERTISEMENT
ADVERTISEMENT

Golang bytes Package

Golang | bytes Package: Here, we are going to learn about the Golang bytes package, its constants, variables, and functions with examples.

In Go language, the byte is an unsigned 8-bit integer (uint8). A byte has a limit of 0 – 255 in the numerical range. It can represent an ASCII character.

In Go language, the bytes package implements functions for the manipulation of byte slices. It is analogous to the facilities of the strings package.

To use these bytes package constants, variables, and functions – we need to import the bytes package.

The import statement:

import "bytes"

List of Golang bytes Package Constants, Variables, and Functions

The bytes package has the following constants, variables, and functions,

Constants

Constant Description Value
bytes.MinRead It returns the minimum slice size passed to a Read call by Buffer.ReadFrom. 512

Variables

Variable Description Value
bytes.ErrTooLarge It indicates that memory cannot be allocated to store data in a buffer. "bytes.Buffer: too large"

Functions

Function Description
bytes.Compare() It compares two byte slices lexicographically and returns an integer value comparing two byte slices.
bytes.Contains() It checks whether the given byte subslice is within byte slice b.
bytes.ContainsAny() It checks whether any of the UTF-8-encoded code points in the given chars are within byte slice b.
bytes.ContainsRune() It checks whether the rune r contained in the UTF-8-encoded byte slice b.
bytes.Count() It counts and returns the number of non-overlapping instances of the given byte slice sep in the byte slice s.
bytes.Equal() It checks whether both byte slices a and b are the same length and contain the same bytes.
bytes.EqualFold() It checks whether the given byte slices s and t (interpreted as UTF-8 strings) are equal under Unicode case-folding, which is a more general form of case-insensitivity.
bytes.Fields() It splits the given byte slice s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace() and returns a slice of subslices of s or an empty slice if s contains only white space.
bytes.FieldsFunc() It splits the given byte slice s at each run of code points c satisfying f(c) and returns a slice of subslices of s. If all code points in s satisfy f(c), or len(s) == 0, an empty slice is returned.
bytes.HasPrefix() It checks whether the byte slice s begins with prefix.
bytes.HasSuffix() It checks whether the byte slice s ends with a suffix.
bytes.Index() It returns the index of the first instance of sep in s, or -1 if sep is not present in s. Where sep and s are byte slices.
bytes.IndexAny() It returns the byte index of the first occurrence in the given byte slice s (interpreted s as a sequence of UTF-8-encoded Unicode code points) of any of the Unicode code points in chars. It returns -1 if chars are empty or if there is no code point in common.
bytes.IndexByte() It returns the index of the first instance of the byte c in the byte slice b.
bytes.IndexFunc() It returns the byte index in the byte slice s (interpreted as a sequence of UTF-8-encoded code points) of the first Unicode code point satisfying f(c).
bytes.IndexRune() It returns the byte index of the first occurrence in the byte slice s (interpreted as a sequence of UTF-8-encoded code points) of the given rune.
bytes.Join() It concatenates the elements of the slice of byte slices (s) to create a new byte slice, the elements concatenate with the given separator.
bytes.LastIndex() It returns the index of the last instance of a byte subslice (sep) in the byte slice (s).
bytes.LastIndexAny() It returns the byte index of the last occurrence in s of any of the Unicode code points in chars.
bytes.LastIndexByte() It returns the index of the last instance of the given byte (c) in the byte slice (s).
bytes.LastIndexFunc() It returns the byte index in the byte slice (s – interpreted as a sequence of UTF-8-encoded code points) of the last Unicode code point satisfying f(c).
bytes.Map() It returns a copy of the byte slice (s) with all its characters modified according to the mapping function.
bytes.Repeat() It returns a new byte slice consisting of count copies of the byte slice (b).
bytes.Replace() It returns a copy of the byte slice (s) with the first n non-overlapping instances of old replaced by new. Where old is the byte slice to be replaced and new is the byte slice to be replaced with.
bytes.ReplaceAll() It returns a copy of the byte slice (s) with all non-overlapping instances of old replaced by new. Where old is the byte slice to be replaced and new is the byte slice to be replaced with.
bytes.Runes() It returns a slice of runes (Unicode code points) equivalent to the byte slice (s).
bytes.Split() It slices the byte slice (s) into all subslices separated by sep and returns a slice of the subslices between those separators.
bytes.SplitAfter() It slices the byte slice (s) into all subslices after each instance of sep and returns a slice of those subslices.
bytes.SplitAfterN() It slices the byte slice (s) into subslices after each instance of sep and returns a slice of those subslices.
bytes.SplitN() It slices the byte slice (s) into subslices separated by sep and returns a slice of the subslices between those separators.
bytes.Title() It returns a copy with all Unicode letters of the byte slice (s) that begin words mapped to their title case.
bytes.ToLower() It returns a copy of the byte slice (s) with all Unicode letters mapped to their lower case.
bytes.ToLowerSpecial() It returns a copy with all the Unicode letters of the byte slice (s – treated as UTF-8-encoded bytes ) mapped to their lower case, giving priority to the special casing rules.
bytes.ToTitle() It returns a copy of the byte slice (s – treated as UTF-8-encoded bytes) with all the Unicode letters mapped to their title case.
bytes.ToTitleSpecial() It returns a copy of the byte slice (s – treated as UTF-8-encoded bytes) with all the Unicode letters mapped to their title case, giving priority to the special casing rules.
bytes.ToUpper() It returns a copy of the byte slice (s) with all Unicode letters mapped to their upper case.
bytes.ToUpperSpecial() It returns a copy of the byte slice (s – treated as UTF-8-encoded bytes) with all the Unicode letters mapped to their upper case, giving priority to the special casing rules.
bytes.ToValidUTF8() It returns a copy of the byte slice (s – treated as UTF-8-encoded bytes) with each run of bytes representing invalid UTF-8 replaced with the bytes in replacement, which may be empty.
bytes.Trim() It returns a subslice of the byte slice (s) by slicing off all leading and trailing UTF-8-encoded code points contained in the string (cutset).
bytes.TrimFunc() It returns a subslice of the byte slice (s) by slicing off all leading and trailing UTF-8-encoded code points c that satisfies f(c).
bytes.TrimLeft() It returns a subslice of the byte slice (s) by slicing off all leading UTF-8-encoded code points contained in the string (cutset).
bytes.TrimLeftFunc() It returns a subslice of the byte slice (s – treated as UTF-8-encoded bytes) by slicing off all leading UTF-8-encoded code points c that satisfies f(c).
bytes.TrimPrefix() It returns the byte slice (s) without the provided leading prefix string. If the byte slice (s) doesn't start with prefix, s is returned unchanged.
bytes.TrimRight() It returns a subslice of the byte (s) by slicing off all trailing UTF-8-encoded code points that are contained in the string (cutset).
bytes.TrimRightFunc() It returns a subslice of the byte slice (s) by slicing off all trailing UTF-8-encoded code points c that satisfy f(c).
bytes.TrimSpace() It returns a subslice of the byte slice (s) by slicing off all leading and trailing white spaces, as defined by Unicode.
bytes.TrimSuffix() It returns the byte slice (s) without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

Example of bytes.Compare() function

// Golang program to demonstrate the
// example of bytes.Compare() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Declaring byte slices
	var a = []byte{10, 20, 30, 70, 120, 255}
	var b = []byte{100, 120, 50, 70, 120, 255}
	var c = []byte{10, 20, 30, 70, 120, 255}

	// Comparing byte slices and printing results
	result := bytes.Compare(a, b)
	if result == 0 {
		fmt.Printf("%d, both a and b are equal\n", result)
	} else if result < 0 {
		fmt.Printf("%d, a is less than b\n", result)
	} else {
		fmt.Printf("%d, a is greater than b\n", result)
	}

	result = bytes.Compare(b, c)
	if result == 0 {
		fmt.Printf("%d, both b and c are equal\n", result)
	} else if result < 0 {
		fmt.Printf("%d, b is less than c\n", result)
	} else {
		fmt.Printf("%d, b is greater than c\n", result)
	}

	result = bytes.Compare(a, c)
	if result == 0 {
		fmt.Printf("%d, both a and c are equal\n", result)
	} else if result < 0 {
		fmt.Printf("%d, a is less than c\n", result)
	} else {
		fmt.Printf("%d, a is greater than c\n", result)
	}
}

Output:

-1, a is less than b
1, b is greater than c
0, both a and c are equal

Example of bytes.Count() function

// Golang program to demonstrate the
// example of bytes.Count() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.Count(
		[]byte("Hello, world!"), []byte("H")))
	fmt.Println(bytes.Count(
		[]byte("Hello, world!"), []byte("o")))
	fmt.Println(bytes.Count(
		[]byte("Hello, world!"), []byte("l")))
	fmt.Println(bytes.Count(
		[]byte("Hello, world!"), []byte("Hello")))
	fmt.Println(bytes.Count(
		[]byte("Hello, world!"), []byte("Hi")))
}

Output:

1
2
3
1
0

Example of bytes.Equal() function

// Golang program to demonstrate the
// example of bytes.Equal() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Comparing strings
	var a string
	var b string

	a = "Hello, world!"
	b = "Hello, world!"

	if bytes.Equal([]byte(a), []byte(b)) == true {
		fmt.Printf("%q and %q are equal\n", a, b)
	} else {
		fmt.Printf("%q and %q are not equal\n", a, b)
	}

	a = "Hello, world!"
	b = "Hello, WORLD!"

	if bytes.Equal([]byte(a), []byte(b)) == true {
		fmt.Printf("%q and %q are equal\n", a, b)
	} else {
		fmt.Printf("%q and %q are not equal\n", a, b)
	}
}

Output:

"Hello, world!" and "Hello, world!" are equal
"Hello, world!" and "Hello, WORLD!" are not equal
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.