Created
August 13, 2012 16:32
-
-
Save darcyliu/3342382 to your computer and use it in GitHub Desktop.
UTF-8 BOM Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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