cgo: control wrapper's close behavior

Also re-organize code.