美文网首页
golang utf8包的使用,判断文件是否是utf8编码

golang utf8包的使用,判断文件是否是utf8编码

作者: 哆啦在这A梦在哪 | 来源:发表于2020-07-22 17:47 被阅读0次

    有条件的同学看一下官方,我这也是那抄的:https://golang.org/pkg/unicode/utf8/#ValidString

    func Valid(p []byte) bool
    Valid reports whether p consists entirely of valid UTF-8-encoded runes.

    package main
    import (
        "fmt"
        "unicode/utf8"
    )
    func main() {
        valid := []byte("Hello, 世界")
        invalid := []byte{0xff, 0xfe, 0xfd}
        fmt.Println(utf8.Valid(valid))
        fmt.Println(utf8.Valid(invalid))
    }
    

    func ValidRune(r rune) bool
    ValidRune reports whether r can be legally encoded as UTF-8. Code points that are out of range or a surrogate half are illegal.

    package main
    
    import (
        "fmt"
        "unicode/utf8"
    )
    
    func main() {
        valid := 'a'
        invalid := rune(0xfffffff)
    
        fmt.Println(utf8.ValidRune(valid))
        fmt.Println(utf8.ValidRune(invalid))
    }
    
    

    相关文章

      网友评论

          本文标题:golang utf8包的使用,判断文件是否是utf8编码

          本文链接:https://www.haomeiwen.com/subject/caytlktx.html