Create a gist now

Instantly share code, notes, and snippets.

UTF-8 BOM Checker
// Check UTF-8 BOM
// go run cbom.go -path=hello.txt
package main
import(
"flag"
"os"
"log"
"fmt"
)
var (
filename = flag.String("path","","filename")
)
func main(){
flag.Parse()
file, err := os.Open(*filename)
if err != nil {
log.Fatal(err)
}
data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
log.Fatal(err)
}
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);
}else{
//fmt.Printf("%x\n",data[0])
//fmt.Printf("%x\n",data[1])
//fmt.Printf("%x\n",data[2])
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