Posts

Showing posts with the label operating-system

Get file size given file descriptor in Go

Image
Clash Royale CLAN TAG #URR8PPP Get file size given file descriptor in Go If given a path, I would use this to get file size file, _ := os.Open(path) fi, _ := file.Stat() fsuze := fi.Size() But if only given fd , how can I get the file size? Is there any way in golang like fd lseek(fd, 0, SEEK_END) in C Can I get File stat without opening the file? Just like C, given path int stat(const char*path, struct stat *statbuf) I just looked into the golang os package and found func Stat(name string) (FileInfo, error) just like C's int stat(const char*path, struct stat *statbuf) Just to clarify, does fd mean an integer file descriptor, as one would get from open() in C? – Jonathon Reinhart 41 mins ago fd open() Yes. I get this fd by calling os.File.stat().Fd() ...

Difference between os.File, io.Reader and os.Stdin

Difference between os.File, io.Reader and os.Stdin I was looking at NewScanner in the official go docs and it mentions the parameter to be passed to bufio.NewScanner should be of type io.Reader . However, the following works for me: bufio.NewScanner io.Reader file, err := os.Open("filename") scanner := bufio.NewScanner(file) The same can be seen for os.Stdin as well. Given this what is the difference between os.File , os.Stdin and io.Reader ? Are they interchangeable? os.Stdin os.File os.Stdin io.Reader io.Reader is an interface. os.File and os.Stdin implement this interface having method Read() and can be used where io.Reader expected – Uvelichitel 31 mins ago @Projjol os.File is a concrete type, your file is a variable of that type, os.Stdin is also a variable of that same type. io.Reader is ...