From 0450ba8421d2b822053bc1ba087f45cdcf8c3a62 Mon Sep 17 00:00:00 2001 From: jim teeuwen Date: Thu, 8 Mar 2012 11:47:57 +0100 Subject: [PATCH] Change signature of generated function to only return the decompressed byte slice. The error value is removed. Instead the function will panic when a decompression error occurs. This allows us to assign the data to global variables were necessary. A decompression error is considered a deal breaker and therefor the panic is warranted. --- README.md | 15 +++++++++++---- bindata.go | 10 ++++++---- testdata/gophercolor.png | Bin testdata/gophercolor.png.go | 8 +++++--- 4 files changed, 22 insertions(+), 11 deletions(-) mode change 100755 => 100644 testdata/gophercolor.png diff --git a/README.md b/README.md index fc5e2d1..1af4773 100644 --- a/README.md +++ b/README.md @@ -19,24 +19,31 @@ converted to a raw byte slice. declaration with name 'main' and one function named 'gophercolor_png'. It looks like this: - func gophercolor_png() ([]byte, error) { + // gophercolor_png returns the decompressed binary data. + // It panics if an error occurred. + func gophercolor_png() []byte { gz, err := gzip.NewReader(bytes.NewBuffer([]byte{ ... })) if err != nil { - return nil, err + panic("Decompression failed: " + err.Error()) } var b bytes.Buffer io.Copy(&b, gz) gz.Close() - return b.Bytes(), nil + return b.Bytes() } You can now simply include the new .go file in your program and call - gophercolor_png() to get the uncompressed image data. + gophercolor_png() to get the uncompressed image data. The function panics + if something went wrong during decompression. This makes any faults appearant + during initialization of your program, so it can quickly be fixed. Additionally, + this approach allows us to assign the decompressed file data to global + variables where necessary. + See the testdata directory for example input and output. Aternatively, you can pipe the input file data into stdin. bindata will then diff --git a/bindata.go b/bindata.go index d13145e..e04c48e 100644 --- a/bindata.go +++ b/bindata.go @@ -21,8 +21,10 @@ import ( "io" ) -func %s() ([]byte, error) { - gz, err := gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname) +// %s returns the decompressed binary data. +// It panics if an error occurred. +func %s() []byte { + gz, err := gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname, funcname) gz := gzip.NewWriter(&GoWriter{Writer: output}) io.Copy(gz, input) @@ -32,14 +34,14 @@ func %s() ([]byte, error) { })) if err != nil { - return nil, err + panic("Decompression failed: " + err.Error()) } var b bytes.Buffer io.Copy(&b, gz) gz.Close() - return b.Bytes(), nil + return b.Bytes() }`) return } diff --git a/testdata/gophercolor.png b/testdata/gophercolor.png old mode 100755 new mode 100644 diff --git a/testdata/gophercolor.png.go b/testdata/gophercolor.png.go index 74b4956..365d19d 100644 --- a/testdata/gophercolor.png.go +++ b/testdata/gophercolor.png.go @@ -6,7 +6,9 @@ import ( "io" ) -func gophercolor_png() ([]byte, error) { +// gophercolor_png returns the decompressed binary data. +// It panics if an error occurred. +func gophercolor_png() []byte { gz, err := gzip.NewReader(bytes.NewBuffer([]byte{ 0x1f,0x8b,0x08,0x00,0x00,0x09,0x6e,0x88,0x00,0xff,0x34,0x9b, 0x65,0x50,0x1b,0x5f,0x17,0xc6,0x43,0x69,0x8b,0xbb,0xbb,0xbb, @@ -1837,12 +1839,12 @@ func gophercolor_png() ([]byte, error) { })) if err != nil { - return nil, err + panic("Decompression failed: " + err.Error()) } var b bytes.Buffer io.Copy(&b, gz) gz.Close() - return b.Bytes(), nil + return b.Bytes() } \ No newline at end of file