TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Bytes: Slices and Methods

Use byte slices and the bytes package. Manipulate, test and benchmark byte slices.
Bytes. A byte is an 8-bit unsigned int. In Go we often use byte slices. And the "bytes" package provides helper methods for byte slices (similar to strings).
We use methods, like append(), to build byte slices. We can specify them with string literals. Methods like bytes.Index help us test and change bytes.
Byte slices. This program introduces byte slices. We create a byte slice from a string literal "abc." We append a byte to a byte slice.

String: For clearer display, we convert a byte slice into string with the string() built-in method.

Len: A byte slice has a length, which we retrieve with len. And we can access individual bytes.

Golang program that introduces byte slices package main import "fmt" func main() { values := []byte("abc") fmt.Println(values) // Append a byte. values = append(values, byte('d')) // Print string representation of bytes. fmt.Println(string(values)) // Length of byte slice. fmt.Println(len(values)) // First byte. fmt.Println(values[0]) } Output [97 98 99] abcd 4 97
Index. Here we import the "bytes" package at the top (in the import statement). We call bytes.Index to locate the sequence with the byte values equal to "dog."

Result: As with strings.Index, bytes.Index returns the index if a sequence matches. Otherwise it returns -1.

Index
Golang program that uses bytes.Index package main import ( "bytes" "fmt" ) func main() { values := []byte("a dog") // Search for this byte sequence. result := bytes.Index(values, []byte("dog")) fmt.Println(result) // This byte sequence is not found. result = bytes.Index(values, []byte("cat")) if result == -1 { fmt.Println("cat not found") } } Output 2 cat not found
Append string. In Go we can use append() on a byte slice with a string argument. We must specify the three periods after the string to append the string.

Warning: This is a special syntax form. If we try to append a string with the three periods following it, we get an error.

Golang program that appends string to byte slice package main import "fmt" func main() { value := []byte("abc") // Append a string to a byte slice with special syntax. value = append(value, "def"...) fmt.Println(value) } Output [97 98 99 100 101 102]
Copy string into bytes. The copy() built-in can copy a string into a byte slice. Here we create an empty 4-element byte slice. Then we copy a three-element string into it.
Golang program that copies string to byte slice package main import "fmt" func main() { // Create an empty byte slice of length 4. values := make([]byte, 4) // Copy string into bytes. animal := "cat" copied := copy(values, animal) fmt.Println(copied) fmt.Println(values) } Output 3 [99 97 116 0]
Buffer. We can use the bytes.Buffer type to quickly append large pieces of byte data together. Many helpful methods, like WriteString, are available.bytes.Buffer
Read bytes in file. With bufio.ScanBytes, we can use Scan() to read each individual byte in a file. We can test and store them as required.bufio.ScanBytes
A review. Byte slices can be used much like strings in the Go language. We can create byte slices from string literals. And the "bytes" package helps us manipulate and test our data.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf