doc.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
  2. // license. Its contents can be found at:
  3. // http://creativecommons.org/publicdomain/zero/1.0/
  4. /*
  5. bindata converts any file into managable Go source code. Useful for
  6. embedding binary data into a go program. The file data is optionally gzip
  7. compressed before being converted to a raw byte slice.
  8. The following paragraphs cover some of the customization options
  9. which can be specified in the Config struct, which must be passed into
  10. the Translate() call.
  11. Debug vs Release builds
  12. When used with the `Debug` option, the generated code does not actually include
  13. the asset data. Instead, it generates function stubs which load the data from
  14. the original file on disk. The asset API remains identical between debug and
  15. release builds, so your code will not have to change.
  16. This is useful during development when you expect the assets to change often.
  17. The host application using these assets uses the same API in both cases and
  18. will not have to care where the actual data comes from.
  19. An example is a Go webserver with some embedded, static web content like
  20. HTML, JS and CSS files. While developing it, you do not want to rebuild the
  21. whole server and restart it every time you make a change to a bit of
  22. javascript. You just want to build and launch the server once. Then just press
  23. refresh in the browser to see those changes. Embedding the assets with the
  24. `debug` flag allows you to do just that. When you are finished developing and
  25. ready for deployment, just re-invoke `go-bindata` without the `-debug` flag.
  26. It will now embed the latest version of the assets.
  27. Lower memory footprint
  28. The `NoMemCopy` option will alter the way the output file is generated.
  29. It will employ a hack that allows us to read the file data directly from
  30. the compiled program's `.rodata` section. This ensures that when we call
  31. call our generated function, we omit unnecessary memcopies.
  32. The downside of this, is that it requires dependencies on the `reflect` and
  33. `unsafe` packages. These may be restricted on platforms like AppEngine and
  34. thus prevent you from using this mode.
  35. Another disadvantage is that the byte slice we create, is strictly read-only.
  36. For most use-cases this is not a problem, but if you ever try to alter the
  37. returned byte slice, a runtime panic is thrown. Use this mode only on target
  38. platforms where memory constraints are an issue.
  39. The default behaviour is to use the old code generation method. This
  40. prevents the two previously mentioned issues, but will employ at least one
  41. extra memcopy and thus increase memory requirements.
  42. For instance, consider the following two examples:
  43. This would be the default mode, using an extra memcopy but gives a safe
  44. implementation without dependencies on `reflect` and `unsafe`:
  45. func myfile() []byte {
  46. return []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a}
  47. }
  48. Here is the same functionality, but uses the `.rodata` hack.
  49. The byte slice returned from this example can not be written to without
  50. generating a runtime error.
  51. var _myfile = "\x89\x50\x4e\x47\x0d\x0a\x1a"
  52. func myfile() []byte {
  53. var empty [0]byte
  54. sx := (*reflect.StringHeader)(unsafe.Pointer(&_myfile))
  55. b := empty[:]
  56. bx := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  57. bx.Data = sx.Data
  58. bx.Len = len(_myfile)
  59. bx.Cap = bx.Len
  60. return b
  61. }
  62. Optional compression
  63. The NoCompress option indicates that the supplied assets are *not* GZIP
  64. compressed before being turned into Go code. The data should still be accessed
  65. through a function call, so nothing changes in the API.
  66. This feature is useful if you do not care for compression, or the supplied
  67. resource is already compressed. Doing it again would not add any value and may
  68. even increase the size of the data.
  69. The default behaviour of the program is to use compression.
  70. Path prefix stripping
  71. The keys used in the `_bindata` map are the same as the input file name
  72. passed to `go-bindata`. This includes the path. In most cases, this is not
  73. desireable, as it puts potentially sensitive information in your code base.
  74. For this purpose, the tool supplies another command line flag `-prefix`.
  75. This accepts a portion of a path name, which should be stripped off from
  76. the map keys and function names.
  77. For example, running without the `-prefix` flag, we get:
  78. $ go-bindata /path/to/templates/
  79. _bindata["/path/to/templates/foo.html"] = path_to_templates_foo_html
  80. Running with the `-prefix` flag, we get:
  81. $ go-bindata -prefix "/path/to/" /path/to/templates/
  82. _bindata["templates/foo.html"] = templates_foo_html
  83. Build tags
  84. With the optional Tags field, you can specify any go build tags that
  85. must be fulfilled for the output file to be included in a build. This
  86. is useful when including binary data in multiple formats, where the desired
  87. format is specified at build time with the appropriate tags.
  88. The tags are appended to a `// +build` line in the beginning of the output file
  89. and must follow the build tags syntax specified by the go tool.
  90. */
  91. package bindata