Go module and command line utility for checking if the given file or data is likely to be binary or text.
binary
utility has overlapping functionality with the file
utility, but with a more limited focus.With Go 1.17 or later:
go install github.com/xyproto/binary/cmd/binary@latest
binary /usr/bin/ls
returns binary
.binary /etc/fstab
returns text
.package main
import (
"fmt"
"os"
"github.com/xyproto/binary"
)
func main() {
filename := os.Args[0]
isBinary, err := binary.File(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
fmt.Printf("%s is a binary file: %v\n", filename, isBinary)
}
The binary.Data
function can be used to determine if a byte slice contains binary data or not.