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.

This commit is contained in:
jim teeuwen 2012-03-08 11:47:57 +01:00
parent 360acac596
commit 0450ba8421
4 changed files with 22 additions and 11 deletions

View File

@ -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

View File

@ -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
}

0
testdata/gophercolor.png vendored Executable file → Normal file
View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -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()
}