How to exclude a directory from build/docs in Go?

In, how can I exclude the example package from go get and the go install binary? It’s also included in the godoc. I’m not sure if excluding examples is necessary in this case, but I will need to exclude a directory in an upcoming project.

Alternatively, will adding another go.mod work to examples work?

I’m not sure if I understood your question correctly, but see if build constraints may help you.

1 Like

A build constraint lists the conditions under which a file should be included in the package.

The examples provided show how to exclude files, but is there a way to specify which files are included using build constraints? Ideally, we aren’t adding build constraints to the examples. I want to prevent the usage of the example packages listed in the godoc in go get and go install; as they aren’t useful to the program.

I was able to do this by adding a go.mod file to the examples directory.