Structs in Fortran

Go’s structs are typed collections of fields. They’re useful for grouping data together to form records.

This person struct type has name and age fields.

module structs_module
    implicit none

    type :: person
        character(len=:), allocatable :: name
        integer :: age
    end type person

contains

    function newPerson(name) result(p)
        character(len=*), intent(in) :: name
        type(person) :: p

        p%name = name
        p%age = 42
    end function newPerson

end module structs_module

newPerson constructs a new person of type person with the given name. Fortran does not have automatic garbage collection like some other languages, so pointers are managed manually.

program main
    use structs_module
    implicit none

    type(person) :: p1, p2, p3
    type(person), pointer :: p_ptr
    type :: dog
        character(len=:), allocatable :: name
        logical :: isGood
    end type dog
    type(dog) :: myDog

    ! This syntax creates a new struct.
    p1 = person('Bob', 20)
    print *, 'Name: ', p1%name, ', Age: ', p1%age

    ! You can name the fields when initializing a struct.
    p2 = person(name='Alice', age=30)
    print *, 'Name: ', p2%name, ', Age: ', p2%age

    ! Omitted fields will be zero-valued.
    p3 = person(name='Fred')
    print *, 'Name: ', p3%name, ', Age: ', p3%age

    ! An association yields a pointer to the struct.
    allocate(p_ptr)
    p_ptr = person(name='Ann', age=40)
    print *, 'Name: ', p_ptr%name, ', Age: ', p_ptr%age

    ! It’s idiomatic to encapsulate new struct creation in constructor functions
    p_ptr => newPerson('Jon')
    print *, 'Name: ', p_ptr%name, ', Age: ', p_ptr%age

    ! Access struct fields with a dot.
    print *, 'Name: ', p1%name

    ! You can also use dots with struct pointers - the pointers are manually dereferenced.
    print *, 'Age: ', p_ptr%age

    ! Structs are mutable.
    p_ptr%age = 51
    print *, 'New Age: ', p_ptr%age

    ! Anonymous struct type
    myDog = dog(name='Rex', isGood=.true.)
    print *, 'Dog Name: ', myDog%name, ', Is Good: ', myDog%isGood

    ! Deallocate pointer
    deallocate(p_ptr)
end program main

To run the Fortran program, compile the code and execute the resulting binary.

$ gfortran -o structs structs.f90
$ ./structs
 Name: Bob , Age: 20
 Name: Alice , Age: 30
 Name: Fred , Age: 0
 Name: Ann , Age: 40
 Name: Jon , Age: 42
 Name: Bob
 Age: 42
 New Age: 51
 Dog Name: Rex , Is Good: T

Now that we can run and compile Fortran programs, let’s learn more about the language.