File.Readdir()不能被多次调用?

When i study Golang, I write the code bellow:

list1,_ := f.Readdir(-1)
len(list1) //some value

list2,_ := f.Readdir(-1)
len(list2) //0

When os.File's method Readdir() called more than once, it will always return an empty []FileInfo.

I want to know why? And how to make my code work?

According to Golang spec for Readdir:

If n > 0, Readdir returns at most n FileInfo structures. In this case, if Readdir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.

It is returning empty because you have reached the file end EOF. If you want to read the same file again. You should move the pointer to the start of the file to read it again.

list1,_ := f.Readdir(-1)
len(list1) //some value

if _, err := f.Seek(0, 0); err != nil { // seek to the start of the file
    panic(err)
}

list2,_ := f.Readdir(-1)
len(list2) // will not return 0

Seeking the file pointer is easy:

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any.