返回了空切片

huangapple go评论85阅读模式
英文:

Empty slice returned

问题

相当新手在学习Go语言。我试图读取目录和子目录中的所有文件,将它们存储在一个切片中,然后进行进一步处理。以下是代码(主要关注MainexpandDirectory函数):

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

const dirname string = "MyDirectory"

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func expandDirectory(currentDirectory string, allFiles []os.FileInfo) []os.FileInfo {
	files, e := ioutil.ReadDir(currentDirectory)
	check(e)
	for _, internalDir := range files {
		switch mode := internalDir.Mode(); {
		case mode.IsDir():
			var filepath = currentDirectory + internalDir.Name() + "\\"
			expandDirectory(filepath, allFiles)
		case mode.IsRegular():
			allFiles = append(allFiles, internalDir)
		}
	}
	return allFiles
}

func main() {
	allFiles := expandDirectory(dirname, make([]os.FileInfo, 5))
	fmt.Printf("%v", cap(allFiles))
}

最后的打印输出将会是初始值5。

只是提醒一下,expandDirectory函数是一个递归函数,用于处理目录,并在处理文件时添加文件引用。

感谢您的帮助。我在哪里出错了?

最终代码(仅更改部分)

func main() {
  allFiles := expandDirectory(dirname)
}

func expandDirectory(currentDirectory string) []os.FileInfo {
	allFiles := make([]os.FileInfo, 0, 5)
    // ...
		case mode.IsDir():
    // ...
			allFiles = append(allFiles, expandDirectory(filepath)...)
	// ...
	return allFiles
}

以上是翻译好的内容,请查阅。

英文:

Fairly new at go.
I am trying to basically read all the files in directory and subdirectory, populate in a slice and then process further.
Here is the code (Main functions to look are Main and expandDirectory

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

const dirname string = "MyDirectory"

func check(e error) {
	if e != nil {
		panic(e)
	}

}

func expandDirectory(currentDirectory string, allFiles []os.FileInfo) []os.FileInfo {
	files, e := ioutil.ReadDir(currentDirectory)
	check(e)
	for _, internalDir := range files {
		switch mode := internalDir.Mode(); {
		case mode.IsDir():
			var filepath = currentDirectory + internalDir.Name() + "\\"
			expandDirectory(filepath, allFiles)
		case mode.IsRegular():
			allFiles = append(allFiles, internalDir)
		}
	}
	return allFiles
}

func main() {
	allFiles := expandDirectory(dirname, make([]os.FileInfo, 5))
	fmt.Printf("%v", cap(allFiles))
}

The final print will print 5 which is the initial value.

Just FYI, the expandDirectory function is just recursive function in case of directories and adding the file reference in case of a file.

Thanks for the help

Where am I going wrong

FINAL CODE (changes Only) <br>

func main() {
  allFiles := expandDirectory(dirname)
}


func expandDirectory(currentDirectory string) []os.FileInfo {
	allFiles := make([]os.FileInfo, 0, 5)
    ....
		case mode.IsDir():
    ....
			allFiles = append(allFiles, expandDirectory(filepath)...)
	....
	return allFiles
}

答案1

得分: 0

你需要将结果附加到其中:

allFiles = append(allFiles, expandDirectory(filepath, allFiles)...)

另外,

allFiles := expandDirectory(dirname, make([]os.FileInfo, 5))

应该改为:

allFiles := expandDirectory(dirname, make([]os.FileInfo, 0, 5))

否则,你将会向allFiles附加内容,而前5个项目将为空。

英文:

You need to append the result to it:

allFiles = append(allFiles, expandDirectory(filepath, allFiles)...)

Also

 allFiles := expandDirectory(dirname, make([]os.FileInfo, 5))

should be:

 allFiles := expandDirectory(dirname, make([]os.FileInfo, 0, 5))

Otherwise you will be appending to allFiles and the first 5 items will be empty.

huangapple
  • 本文由 发表于 2015年7月16日 08:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/31443356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定