String Formatting in Fortran

module point_module
  implicit none
  type :: point
    integer :: x, y
  end type point
end module point_module

program string_formatting
  use point_module
  implicit none
  
  type(point) :: p
  character(len=100) :: s
  
  p = point(1, 2)
  
  ! Fortran doesn't have a direct equivalent to Go's Printf.
  ! We'll use write statements with formatted output instead.
  
  ! Basic structure printing
  write(*, '(A,I0,A,I0,A)') 'struct1: (', p%x, ',', p%y, ')'
  
  ! Fortran doesn't have a direct equivalent to %+v or %#v
  
  ! Type printing
  write(*, '(A)') 'type: point'
  
  ! Boolean formatting
  write(*, '(A,L1)') 'bool: ', .true.
  
  ! Integer formatting
  write(*, '(A,I0)') 'int: ', 123
  
  ! Binary representation
  write(*, '(A,B0)') 'bin: ', 14
  
  ! Character from integer
  write(*, '(A,A1)') 'char: ', achar(33)
  
  ! Hexadecimal
  write(*, '(A,Z0)') 'hex: ', 456
  
  ! Float formatting
  write(*, '(A,F0.6)') 'float1: ', 78.9
  write(*, '(A,ES12.6)') 'float2: ', 123400000.0
  write(*, '(A,ES12.6)') 'float3: ', 123400000.0
  
  ! String formatting
  write(*, '(A,A)') 'str1: ', '"string"'
  write(*, '(A,A)') 'str2: ', '"""string"""'
  
  ! Hexadecimal representation of a string
  write(*, '(A,Z2.2,Z2.2,Z2.2,Z2.2,Z2.2,Z2.2,Z2.2,Z2.2)') 'str3: ', &
       iachar('h'), iachar('e'), iachar('x'), iachar(' '), &
       iachar('t'), iachar('h'), iachar('i'), iachar('s')
  
  ! Pointer (address) printing isn't straightforward in Fortran
  
  ! Width formatting for integers
  write(*, '(A,I6,I6)') 'width1: |', 12, 345
  
  ! Width and precision for floats
  write(*, '(A,F6.2,F6.2)') 'width2: |', 1.2, 3.45
  
  ! Left-justified floats
  write(*, '(A,F6.2,F6.2)') 'width3: |', 1.2, 3.45
  
  ! Width formatting for strings
  write(*, '(A,A6,A6)') 'width4: |', 'foo', 'b'
  
  ! Left-justified strings
  write(*, '(A,A6,A6)') 'width5: |', 'foo', 'b'
  
  ! Fortran doesn't have a direct equivalent to Sprintf
  ! We can use internal writes to achieve similar functionality
  write(s, '(A,A)') 'sprintf: a ', 'string'
  write(*, '(A)') trim(s)
  
  ! Writing to standard error
  write(0, '(A,A)') 'io: an ', 'error'

end program string_formatting

This Fortran program demonstrates various string formatting techniques that are analogous to the Go example. However, there are some key differences:

  1. Fortran uses write statements for output, which are more verbose than Go’s Printf.
  2. Fortran doesn’t have direct equivalents for some of Go’s formatting verbs (like %+v or %#v).
  3. Pointer (address) printing isn’t straightforward in Fortran.
  4. Fortran doesn’t have a direct equivalent to Sprintf. We use internal writes to achieve similar functionality.
  5. Some formatting options (like left-justification) are specified in the format string rather than with flags.

To compile and run this Fortran program:

$ gfortran string_formatting.f90 -o string_formatting
$ ./string_formatting

The output will be similar to the Go program, with some differences due to the limitations and different syntax of Fortran.