Barring some exceptions like _test packages, multiple files in the same directory will be the same package. If you want to have a bunch of examples in subfolders, I often create a go module in the parent folder and then run them with relative paths. I wrote about this recently on another question here.
From the docs:
Multiple programs in the same repository will typically have separate directories:
project-root-directory/ go.mod internal/ ... shared internal packages prog1/ main.go prog2/ main.goIn each directory, the program’s Go files declare
package main. A top-levelinternaldirectory can contain shared packages used by all commands in the repository.Users can install these programs as follows:
$ go install github.com/someuser/modname/prog1@latest $ go install github.com/someuser/modname/prog2@latest
So for example check out the example code from the book The Go Programming Language. Try the following:
# Clone that repo (note how many subfolders it has)
git clone https://github.com/adonovan/gopl.io.git
# Switch to newly-cloned folder
cd gopl.io
# Run one of the examples using relative path
go run ./ch1/helloworld
# Can also run with module name in go.mod:
go run gopl.io/ch1/helloworld