Struct Embedding in Fortran
Fortran doesn’t have a direct equivalent to struct embedding as in Go, but we can achieve similar functionality using modules and derived types. Here’s an explanation of the Fortran code:
We define a
base_module
that contains thebase_type
and thedescribe
function.The
container_module
uses thebase_module
and defines acontainer_type
that includes abase_type
as a component.In the main program, we create a
container_type
variable and set its values.We can access the
num
field directly through thebase
component of thecontainer_type
.We can call the
describe
function with thebase
component of thecontainer_type
.Fortran doesn’t have interfaces like Go, so we can’t directly implement the
describer
interface. Instead, we simulate it by calling thedescribe
function directly.
To run the program, save it as struct_embedding.f90
and compile it using a Fortran compiler:
This Fortran code demonstrates a similar concept to struct embedding in Go, although the implementation details differ due to the language’s characteristics.