-
-
Notifications
You must be signed in to change notification settings - Fork 9
Namespaces
IsaacShelton edited this page Mar 21, 2022
·
1 revision
Namespaces can be used to group functionality under a common name
Unlike most other languages, all namespaced items must always be referenced using their fully qualified name
namespace my_namespace
namespace my_namespace {
}
A scope can optionally be provided by using { /* ... */ }
. If none is provided, then the namespace will contain everything until the end of the file or until another namespace is created.
In order to access symbols defined within a namespace, you can use either
my_namespace\my_symbol
or
my_namespace:my_symbol
import basics
namespace quickmath {
func sum(a, b int) int {
return a + b
}
}
func main {
printf(“21 = %d\n“, quickmath\sum(8, 13))
printf(“42 = %d\n”, quickmath:sum(19, 23))
}
Namespaces can be nested by using the name of a namespace within the name of another namespace
namespace math\quickmath\addition {
func sum(a, b int) int {
return a + b
}
}
func main {
printf(“3 = %d\n”, math\quickmath\addition\sum(1, 2))
}
Symbols can be contained in a namespace even when they aren’t inside a namespace’s scope. In order to achieve this, you can use the full namespaced name when declaring the symbol.
import basics
func quickmath\sum(a, b int) int {
return a + b
}
func main {
print(“5 = %d\n”, quickmath\sum(2, 3))
}