Namespaces and use
Demonstrates namespace organization and importing; each language maps the concept to modules or packages.
C C23
#include <stdio.h>
// C has no namespaces; the "name prefix" convention assigns symbol ownership (equivalent to PHP's Lib namespace)
const char *lib_greet(void) {
return "Greeting from Lib";
}
// using within the "App namespace": the prefix already shows ownership, call directly, no use needed
int main(void) {
printf("%s\n", lib_greet());
printf("%s", lib_greet());
return 0;
}
Greeting from Lib Greeting from Lib
C++ C++26
#include <iostream>
#include <string>
namespace Lib { // namespace definition
std::string greet() {
return "Greeting from Lib";
}
}
int main() {
std::cout << Lib::greet() << "\n"; // qualified-name access
using Lib::greet; // a using declaration brings in a single name
std::cout << greet() << "\n";
return 0;
}
Greeting from Lib Greeting from Lib
C# v11.0.100
using Lib; // using imports a namespace (must precede top-level statements)
Console.WriteLine(Greeter.Greet());
Console.WriteLine(Lib.Greeter.Greet()); // fully qualified name
namespace Lib { // namespace declaration placed after top-level statements
static class Greeter {
public static string Greet() => "Greeting from Lib";
}
}
Greeting from Lib Greeting from Lib
Go v1.27.4
package main
import (
"fmt"
m "math" // import a package with an alias
)
// the package name is the namespace: access as package.Identifier after import
func main() {
fmt.Println(m.Pi)
fmt.Println(m.Sqrt(16))
}
3.141592653589793 4
Java v27.0.1
import java.util.ArrayList; // import a single class
import java.util.List;
import static java.lang.Math.max; // static import: call methods by bare name
// in real projects this file would start with package com.example;
// the package declaration is omitted so a single file can run (javac Main.java)
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
System.out.println(list.size());
System.out.println(max(3, 7));
System.out.println(java.util.Collections.emptyList().isEmpty()); // fully-qualified name
}
}
1 7 true
JavaScript v26.13.1
// in browsers/bundlers (Vite/webpack) the real syntax is import/export modules;
// the validation environment runs this as a script, so an object emulates the module namespace:
const Lib = (() => { // emulate a module: private variables + an exported object
const version = "1.0";
return { greet: () => `Greeting from Lib v${version}` };
})();
const { greet } = Lib; // like import { greet } from "./lib.js"
console.log(greet());
console.log(Lib.greet()); // namespaced access, like Lib.greet()
Greeting from Lib v1.0 Greeting from Lib v1.0
PHP v8.6.0
namespace Lib {
function greet(): string {
return "Greeting from Lib";
}
}
namespace App {
use function Lib\greet; // import a function (or use Lib\ClassName)
echo greet(), "\n";
echo \Lib\greet(); // fully qualified name: starts from the global root
}
Greeting from Lib Greeting from Lib
Python v3.15.2
import math # import the whole module (a module is a namespace)
from math import sqrt # import only the specified members
import math as m # alias it
print(math.pi)
print(int(sqrt(16)))
print(m.floor(3.7))
3.141592653589793 4 3
Rust v1.99.1
// inline mod block for demo; in real projects a mod declaration maps to a same-named file (src/lib.rs, etc.)
mod lib {
pub fn greet() -> &'static str { // pub makes it visible outside
"Greeting from the lib module"
}
}
use lib::greet; // bring greet into the current scope
fn main() {
println!("{}", greet());
println!("{}", lib::greet()); // path access
}
Greeting from the lib module Greeting from the lib module
TypeScript v26.13.1
// a module is the namespace: export/import resolved by the compiler (an object emulates the module here)
const Lib = (() => { // emulate a module: private variables + an exported object
const version = "1.0";
return { greet: () => `Greeting from Lib v${version}` };
})();
const { greet } = Lib; // like import { greet } from "./lib"
console.log(greet());
console.log(Lib.greet()); // namespaced access
Greeting from Lib v1.0 Greeting from Lib v1.0
Language differences
C: no namespaces — naming prefixes group symbols (a fully qualified name is just the prefixed function name).
Go: packages are namespaces, and import supports aliases.
Java: package + import (including static imports; the example omits the package declaration to stay runnable).
JavaScript: the counterpart is ES modules import/export, with paths resolved by bundlers or by the browser via <script type="module"> (the example simulates with objects).
PHP: namespace declares which namespace a piece of code belongs to; use imports classes/functions/constants, supports aliases and fully qualified names like \A\B (the example uses brace syntax to demo two namespaces in one file).
Python: modules are namespaces natively — import imports modules, from...import imports members, and as creates aliases.
Rust: declares modules with mod (organized by file/directory); use brings items into scope; items are private by default and exported with pub.