Menu Close

Go – How to convert byte slice to upper case in go

Here, bytes.ToUpper() function is used to convert byte slice to upper case in go. It returns the copy of slice of bytes in upper case.

ToUpper() built-in function of bytes package converts slice of bytes with all Unicode letters mapped to their upper case.

bytes.ToUpper() function prototype:

func ToUpper(s []byte) []byte

Input parameters:
s: slice of bytes

Return:
It returns the copy of slice of bytes in upper case.

Explanation on how to convert byte slice to upper case in go

1)
s := []byte("TechieIndoor")

Output: TECHIEINDOOR

2)
s := []byte("HELLO")

Output: HELLO

3)
s := []byte{10, 20}

Output: {10, 20}

Example:

Code 1:

package main

import (
	"bytes"
	"fmt"
)

// main function
func main() {
    
    s := []byte("TechieIndoor")
    
    fmt.Printf("%q", bytes.ToUpper(s))
    
    
    s = []byte("HELLO")
    
    fmt.Printf("\n%q", bytes.ToUpper(s))
    
}

Output:

"TECHIEINDOOR"
"HELLO"

To learn more about golang, Please refer given below link:

References:

https://golang.org/doc/

Posted in bytes, golang, packages

Leave a Reply

Your email address will not be published. Required fields are marked *