├── .gitignore ├── 2goarray.go ├── LICENSE ├── README.md ├── unix.go └── windows.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | 2goarray 24 | -------------------------------------------------------------------------------- /2goarray.go: -------------------------------------------------------------------------------- 1 | // Simple utility to convert a file into a Go byte array 2 | 3 | // Clint Caywood 4 | 5 | // http://github.com/cratonica/2goarray 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | "io" 11 | "os" 12 | ) 13 | 14 | const ( 15 | NAME = "2goarray" 16 | VERSION = "0.1.0" 17 | URL = "http://github.com/cratonica/2goarray" 18 | GENERATED_BY = "// File generated by " + NAME + " v" + VERSION + " (" + URL + ")" 19 | ) 20 | 21 | func main() { 22 | if len(os.Args) != 3 { 23 | fmt.Print(NAME + " v" + VERSION + "\n\n") 24 | fmt.Println("Usage: " + NAME + " array_name package_name") 25 | return 26 | } 27 | 28 | if isTerminal() { 29 | fmt.Println("\nPlease pipe the file you wish to encode into stdin\n") 30 | return 31 | } 32 | 33 | fmt.Println(GENERATED_BY + "\n") 34 | fmt.Printf("package %s\n\n", os.Args[2]) 35 | fmt.Printf("var %s []byte = []byte{", os.Args[1]) 36 | buf := make([]byte, 1) 37 | var err error 38 | var totalBytes uint64 39 | var n int 40 | for n, err = os.Stdin.Read(buf); n > 0 && err == nil; { 41 | if totalBytes%12 == 0 { 42 | fmt.Printf("\n\t") 43 | } 44 | fmt.Printf("0x%02x, ", buf[0]) 45 | totalBytes++ 46 | n, err = os.Stdin.Read(buf) 47 | } 48 | if err != nil && err != io.EOF { 49 | fmt.Errorf("Error: %v", err) 50 | } 51 | fmt.Print("\n}\n\n") 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Clint Caywood 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2goarray 2 | ======== 3 | A simple utility to encode a file (or any other data) into a Go byte slice. 4 | 5 | Having [set up your Go environment](http://golang.org/doc/install), simply run 6 | 7 | go get github.com/cratonica/2goarray 8 | 9 | Then use by piping a file into the utility and capturing the output. You must provide a name for the generated slice symbol and package name. For example: 10 | 11 | $GOPATH/bin/2goarray MyArray mypackage < myimage.png > myimage.go 12 | 13 | This will output something like: 14 | 15 | package mypackage 16 | 17 | var MyArray []byte = []byte { 18 | 0x49, 0x20, 0x63, 0x61, 0x6e, 0x27, 0x74, 0x20, 0x62, 0x65, 0x6c, 0x69, 19 | 0x65, 0x76, 0x65, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x61, 0x63, 0x74, 0x75, 20 | 0x61, 0x6c, 0x6c, 0x79, 0x20, 0x64, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x64, 21 | 0x20, 0x74, 0x68, 0x69, 0x73, 0x2e, 0x20, 0x4b, 0x75, 0x64, 0x6f, 0x73, 22 | 0x20, 0x66, 0x6f, 0x72, 0x20, 0x62, 0x65, 0x69, 0x6e, 0x67, 0x20, 0x74, 23 | 0x68, 0x6f, 0x72, 0x6f, 0x75, 0x67, 0x68, 0x2e, 0x0a, 24 | } 25 | 26 | ## Contributors 27 | - [Clint Caywood](https://github.com/cratonica) 28 | - [Paul Vollmer](https://github.com/paulvollmer) 29 | -------------------------------------------------------------------------------- /unix.go: -------------------------------------------------------------------------------- 1 | // +build linux darwin 2 | 3 | package main 4 | 5 | import ( 6 | "golang.org/x/crypto/ssh/terminal" 7 | "syscall" 8 | ) 9 | 10 | func isTerminal() bool { 11 | return terminal.IsTerminal(syscall.Stdin) 12 | } 13 | -------------------------------------------------------------------------------- /windows.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | package main 4 | 5 | func isTerminal() bool { 6 | return false 7 | } 8 | --------------------------------------------------------------------------------