- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a slice of bytes in uppercase in Golang?
In Golang, a byte slice is a sequence of bytes. A slice of bytes can be created using the built-in function []byte(). Sometimes, you may want to convert a slice of bytes to uppercase, which means converting all the letters to their uppercase equivalents. This can be easily achieved using the bytes.ToUpper() function or the strings.ToUpper() function. In this article, we will learn how to convert a slice of bytes to uppercase in Golang.
Using bytes.ToUpper()
The bytes.ToUpper() function converts all the ASCII letters in a slice of bytes to their uppercase equivalents. Here's how to use it to convert a slice of bytes to uppercase −
Example
Here's an example −
package main import ( "bytes" "fmt" ) func main() { s := []byte("hello world") fmt.Println("Original:", string(s)) // Output: Original: hello world s = bytes.ToUpper(s) fmt.Println("Uppercase:", string(s)) // Output: Uppercase: HELLO WORLD }
Output
Original: hello world Uppercase: HELLO WORLD
In this example, we create a slice of bytes s with the value "hello world". We then pass the slice to the bytes.ToUpper() function to convert it to uppercase. The bytes.ToUpper() function returns a new slice of bytes with all the ASCII letters in uppercase. We then assign the new slice back to s and print the original and uppercase versions of the slice using the fmt.Println() function.
Using strings.ToUpper()
The strings.ToUpper() function converts all the ASCII letters in a string to their uppercase equivalents. Here's how to use it to convert a slice of bytes to uppercase −
Example
package main import ( "fmt" "strings" ) func main() { s := []byte("hello world") fmt.Println("Original:", string(s)) // Output: Original: hello world s = []byte(strings.ToUpper(string(s))) fmt.Println("Uppercase:", string(s)) // Output: Uppercase: HELLO WORLD }
Output
Original: hello world Uppercase: HELLO WORLD
In this example, we create a slice of bytes s with the value "hello world". We then convert the slice to a string using the string() function and pass it to the strings.ToUpper() function to convert it to uppercase. The strings.ToUpper() function returns a new string with all the ASCII letters in uppercase. We then convert the new string back to a slice of bytes using the []byte() function and assign it back to s. Finally, we print the original and uppercase versions of the slice using the fmt.Println() function.
Conclusion
In this article, we learned how to convert a slice of bytes to uppercase in Golang using the bytes.ToUpper() function and the strings.ToUpper() function. Both functions are easy to use and efficient. The bytes.ToUpper() function is recommended if you are working with slices of bytes, while the strings.ToUpper() function is recommended if you have converted the slice of bytes to a string.