Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
UTF-8 BOM Checker
// Check UTF-8 BOM
// go run cbom.go -path=hello.txt
package main
var (
filename = flag.String("path","","filename")
func main(){
file, err := os.Open(*filename)
if err != nil {
data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
if(count>3&& data[0] != 0xef || data[1] != 0xbb || data[2] != 0xbf){
fmt.Printf("%s is not a UTF-8 file, or a UTF-8 file without BOM.\n",*filename);
fmt.Printf("%s is a UTF-8 file with BOM.\n",*filename);
//fmt.Printf("read %d bytes: %q\n", count, data[:count])
defer file.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment