Go Trick: Vendoring Non-Package Directory

After go version 1.13+, go mod -vendor prunes non-package directories from vendor. This is noted here: https://github.com/golang/go/issues/26366

So, how do we include a non-package directory(sql migrations, yamls etc.) to be included in the vendor ?

Example directory:

1
2
3
4
migrations
    create_table.sql.up
    create_table.sql.down
    migrations.go

migrations.go:

1
2
// +build migrations
package migrations

The conditionally compiled file migrations.go will force go mod -vendor to include it.