In the file "os" in the package "os" there is such code:

type File struct { *file } type file struct { fd syscall.Handle name string dirinfo *dirInfo l sync.Mutex isConsole bool lastbits []byte readbuf []rune } 

Questions on the first struct File:

  1. Why so written this strukt? (I would just be the second Struck in the plan and everything, why is the first one here?)
  2. What keeps this struct? There is no field in it, for example:

     f *file // а там написано просто *file 
  3. How to access a field through a file?

     var x File fmt.Println(x.name) // ошибка 

    1 answer 1

    File contains a pointer to the file structure. The file structure itself is not exportable (it starts with a small letter, unlike File ), just like the structure fields.

    In the source code itself it is written that this is done so that the fields of the structure certainly cannot be addressed directly. Because file is an open file descriptor; overwriting its fields directly can lead to unexpected consequences.

    To access, for example, the file name is a function .Name() .

    I also add that "under the hood" functions from os to change any file attributes (for example, changing rights via .Chmod() ) is done via system calls ( package syscall ).