Last active
October 27, 2017 00:05
-
-
Save spiegel-im-spiegel/93961aa367a94b79984b0a61964e3114 to your computer and use it in GitHub Desktop.
バイナリデータを Golang の []byte リテラル表記に変換する簡単なお仕事 ref: http://qiita.com/spiegel-im-spiegel/items/272c1b8c01eb287059e0
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
package godump | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
) | |
//DumpBytes returns []bytes literal string | |
func DumpBytes(r io.Reader, name string) (io.Reader, error) { | |
buf := new(bytes.Buffer) | |
b := make([]byte, 1) | |
var err error | |
sep := fmt.Sprintf("var %s = []byte{", name) | |
for true { | |
if _, err = r.Read(b); err != nil { | |
break | |
} | |
fmt.Fprintf(buf, "%s%#02x", sep, b) | |
sep = ", " | |
} | |
fmt.Fprintln(buf, "}") | |
return buf, nil | |
} |
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
$ go get github.com/spiegel-im-spiegel/godump |
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
$ go get github.com/spiegel-im-spiegel/godump |
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
dump, err := godump.DumpBytes(bytes.NewBufferString("hello world"), "foobar") | |
if err != nil { | |
return | |
} | |
io.Copy(os.Stdout, dump) | |
// Output: | |
// var foobar = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64} |
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
$ dep ensure -add github.com/spiegel-im-spiegel/godump |
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
dump, err := godump.DumpBytes(bytes.NewBufferString("hello world"), "foobar") | |
if err != nil { | |
return | |
} | |
io.Copy(os.Stdout, dump) | |
// Output: | |
// var foobar = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64} |
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
$ godump -h | |
Usage: | |
godump [flags] [binary file] | |
Flags: | |
-h, --help help for godump | |
-n, --name string value name (default "dumpList") |
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
$ godump -h | |
Usage: | |
godump [flags] [binary file] | |
Flags: | |
-h, --help help for godump | |
-n, --name string value name (default "dumpList") |
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
$ echo hello world | godump -n foobar | |
var foobar = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0a} |
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
$ cat input.txt | |
hello world | |
$ godump -n foobar input.txt | |
var foobar = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment