Chapter 13

Low-Level

unsafe and cgo

✓ Use unsafe ✓ Call C code
1
Bzip
The version of this program that appeared in the first and second printings did not comply with the proposed rules for passing pointers between Go and C, described here: https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md The rules forbid a C function like bz2compress from storing 'in' and 'out' (pointers to variables allocated by Go) into the Go variable 's', even temporarily. The version below, which appears in the third printing, has been corrected. To comply with the rules, the bz_stream variable must be allocated by C code. We have introduced two C functions, bz2alloc and bz2free, to allocate and free instances of the bz_stream type. Also, we have changed bz2compress so that before it returns, it clears the fields of the bz_stream that contain pointers to Go variables. Package bzip provides a writer that uses bzip2 compression (bzip.org).
p.362 +20 XP advanced
2
Bzip Print
This is the version that appears in print, but it does not comply with the proposed rules for passing pointers between Go and C. (https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md) See gopl.io/ch13/bzip for an updated version. Package bzip provides a writer that uses bzip2 compression (bzip.org).
p.362 +20 XP advanced
3
Bzipper
Bzipper reads input, bzip2-compresses it, and writes it out.
p.365 +20 XP advanced
4
Equal
Package equal provides a deep equivalence relation for arbitrary values.
p.359 +20 XP advanced
5
Unsafeptr
Package unsafeptr demonstrates basic use of unsafe.Pointer.
p.357 +20 XP advanced