The Unicode package

The unicode standard Go package contains various handy functions. One of them, which is called unicode.IsPrint(), can help you identify the parts of a string that are printable using runes. This technique will be illustrated in the Go code of unicode.go, which will be presented in two parts.

The first part of unicode.go is as follows:

package main 
 
import ( 
    "fmt" 
    "unicode" 
) 
 
func main() { 
    const sL = "x99x00abx50x00x23x50x29x9c" 

The second code segment of unicode.go is shown in the following Go code:

    for i := 0; i < len(sL); i++ { 
        if unicode.IsPrint(rune(sL[i])) { 
            fmt.Printf("%c
", sL[i]) 
        } else { 
            fmt.Println("Not printable!") 
        } 
    } 
} 

As stated before, all of the dirty work is done by the unicode.IsPrint() function that returns true when a rune is printable and false otherwise. If you are really into Unicode characters, you should definitely check the documentation page of the unicode package.

Executing unicode.go will generate the following output:

$ go run unicode.go
Not printable!
Not printable!
a
b
P
Not printable!
#
P
)
Not printable!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107