This is a creation in Article, where the information may have evolved or changed.
I want to write a function like this:
Access all *.db files in a directory. And put in filepath[]. FilePath is a struct:
type filepath struct { fullpath string //文件路径名 filename string //文件名}
The finished program is this way, when printing:
package mainimport ( "io/ioutil" "strings" "path")const rootpath string = "D://testfile/2345"type filepath struct { fullpath string filename string}func main() { var files []filepath files = make([]filepath, 0, 1) //dir,_:=ioutil.ReadDir(rootpath) //递了个归 readdir(rootpath, files) for _, v := range files { println(v.filename, v.fullpath) }}func readdir(dirs string, files []filepath) { dir, _ := ioutil.ReadDir(dirs) for _, v := range dir { if v.IsDir() { dir2 := path.Join(dirs, v.Name()) readdir(dir2, files) } else { if strings.HasSuffix(v.Name(), ".db") { var str string = v.Name() files = append(files, filepath{fullpath: path.Join(dirs, str), filename: str}) } } }}//输出:(什么~~都没有 一脸懵b.jpg)
A JB-style boy like me, a scripting language boy, has this kind of error when it doesn't use pointers. Add some information to debug?
//把最后一个else 里加两句,变成下面的:else { if strings.HasSuffix(v.Name(), ".db") { str := v.Name() println("老地址", files) files = append(files, filepath{fullpath: path.Join(dirs, str), filename: str}) println("新地址", files) //println(len(files) ) }//输出如下(一对一对看还是很方便 []里的数字含义为 [len/cap]):老地址 [0/1]0xc042033f58新地址 [1/1]0xc042033f58老地址 [1/1]0xc042033f58新地址 [2/2]0xc042054b00老地址 [2/2]0xc042054b00新地址 [3/4]0xc042066b00老地址 [3/4]0xc042066b00新地址 [4/4]0xc042066b00老地址 [4/4]0xc042066b00新地址 [5/8]0xc042036300老地址 [5/8]0xc042036300新地址 [6/8]0xc042036300老地址 [6/8]0xc042036300新地址 [7/8]0xc042036300老地址 [7/8]0xc042036300新地址 [8/8]0xc042036300
Analysis, according to the urine of append, each time the memory is not enough to add Len space, will not generate a period of memory, reassigned to the pointer???
First confirm will not assign!!!!
It's a total of eight append.
- Just one space, just right
- There's not enough space, and Len is now 2.
- Space is not enough, Len is now 4.
- Enough
- Not enough to add Len 8 of
- Enough
- Enough
- Enough
The memory pointer has changed 3 times, so you can be sure that the first files = make ([]filepath, 0, 1) should have one set of data.
Then print the memory, println the position.
Func main () {var files []filepath files = make ([]filepath, 0, 1)//dir,_:=ioutil. ReadDir (RootPath)//handed a return to println ("old Address", files) ReadDir (RootPath, files) for _, V: = Range Files {print ln (v.filename, V.fullpath)}}func readdir (dirs string, files []filepath) {println ("new Address", files) dir, _: = Ioutil . ReadDir (dirs) for _, V: = range dir {if V.isdir () {dir2: = path. Join (dirs, V.name ()) Readdir (Dir2, files)} else {if strings. Hassuffix (V.name (), ". db") {str: = V.name () files = append (files, Filepath{fullpath:path. Join (dirs, str), filename:str})//println (Len (Files)}})}}//print information as follows: Old address [0/1]0xc042 033f58 new Address [0/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [ 1/1]0xc042033f58 new Address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1] 0xc042033f58 new Address [1/1]0xc042033f58 new Address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1] 0xc042033f58 new Address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1]0xc042033f58 new address [1/1] 0xc042033f58 new Address [1/1]0xc042033f58 new address [1/1]0xc042033f58
I found that the head pointer is still the head pointer, so the conclusion:
As the append increases, the head pointer is still the head pointer
Then the question comes, why is not print out!!!
Is there a hole in the Append and nothing to add?
Write a small program and try it.
package mainfunc main() { var p1 []int=[]int{1,2,3,4,5} change(p1) println(p1[5])}func change(p1 []int) { p1=append(p1,2222)}//输出panic :数组越界
At this point, I really was ....
I suddenly think of something!!!!
Suddenly understood, P1 is a slice, and the P1 in main always points to {1,2,3,4,5} without pointing to {1,2,3,4,5,2222}.
The program is modified as follows:
package mainfunc main() { var p1 []int=[]int{1,2,3,4,5} change2(&p1) println(p1[5])}func change2(p1 *[]int) { *p1=append(*p1,2222)}//输出: 2222
Here, I have another thought of the near perfect debugging information, is this paragraph:
Analysis, according to the urine of append, each time the memory is not enough to add Len space, will not generate a period of memory, reassigned to the pointer???
First confirm will not assign!!!!
It's a total of eight append.
- Just one space, just right
- There's not enough space, and Len is now 2.
- Space is not enough, Len is now 4.
- Enough
- Not enough to add Len 8 of
- Enough
- Enough
- Enough
I guess the 99.9% is probably 一个文件夹中有8个.db文件 I went to see it, really.
I think of two solutions:
- Change the pointer (to TMD) like the above function
- Returns a new slice
Finally I choose 2, others seem to be convenient point, so the whole program becomes:
package mainimport ( "io/ioutil" "strings" "path")const rootpath string = "D://testfile/2345"type filepath struct { fullpath string filename string}func main() { var files []filepath files = make([]filepath, 0, 1) files= readdir(rootpath, files) for _, v := range files { println(v.filename, v.fullpath) }}func readdir(dirs string, files []filepath) []filepath { dir, _ := ioutil.ReadDir(dirs) for _, v := range dir { if v.IsDir() { dir2 := path.Join(dirs, v.Name()) files=readdir(dir2, files) } else { if strings.HasSuffix(v.Name(), ".db") { str := v.Name() files = append(files, filepath{fullpath: path.Join(dirs, str), filename: str}) //println(len(files) ) } } } return files}
Again go Path bar, feel a little bit, and do not go.getpath this function, can only manually record the file path.
Alas, the program is finished, if I write a question, I hope you correct.