Importing Subpackages with Go
When working with Go modules, you may encounter situations where you need to import multiple subpackages from a parent directory. The conventional approach is to import each subpackage individually, as seen in the example:
package main import "one/entities/bar/sub1" import "one/entities/bar/sub2" func main() { }
However, you may desire a more concise solution, such as importing all subpackages under a single namespace. This is not directly feasible in Go as the import syntax requires explicit specification of package names or paths.
// Invalid Syntax: import bar "one/entities/bar/*"
Go's import statement demands a specific package name or path to determine the source of imported elements. As such, wildcard imports are not supported in the language.
Ultimately, the most viable option is to manually import each required subpackage:
package main import ( "log" "one/entities/bar/sub1" "one/entities/bar/sub2" ) func main() { v := sub1.GetVar() log.Fatal(v) }
The above is the detailed content of Can You Import All Subpackages Under a Single Namespace in Go?. For more information, please follow other related articles on the PHP Chinese website!