Skip to content

Instantly share code, notes, and snippets.

@darcyliu
Created August 13, 2012 16:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darcyliu/3342382 to your computer and use it in GitHub Desktop.
Save darcyliu/3342382 to your computer and use it in GitHub Desktop.
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