在Golang中,如何将循环中的结果添加到未知大小的动态数组中?

问题描述:

The example I'm working with right now takes input from console asking for a file extension, say .txt . It searches the current directory for files and then does a Println which returns all the files with the .txt onto the screen.

For each result returned, how can I put each filename into an array (or a slice?) and then access each value later in the program to manipulate each file.

It doesn't need to be sequential.

This is the working code (modified from Adam Ng, I think)

     package main

  import (
      "fmt"
      "os"
      "path/filepath"
    "bufio"
    //"bytes"
    //"io/ioutil"

  )

  func main() {


    lineScan := bufio.NewScanner(os.Stdin)
    var inputText string

    fmt.Print("Enter file extension to search for: .extension 
")     
    lineScan.Scan()
    inputText = lineScan.Text()

      dirname := "." + string(filepath.Separator)

      d, err := os.Open(dirname)
      if err != nil {
          fmt.Println(err)
          os.Exit(1)
      }
      defer d.Close()

      files, err := d.Readdir(-1)
      if err != nil {
          fmt.Println(err)
          os.Exit(1)
      }

      for _, file := range files {
          if file.Mode().IsRegular() {

              if filepath.Ext(file.Name()) == inputText {

                fmt.Println(file.Name())
              }
          }
      }
  }

我现在使用的示例从控制台获取输入,要求文件扩展名,例如.txt。 它在当前目录中搜索文件,然后执行Println,将带有.txt的所有文件返回到屏幕上。 p>

对于返回的每个结果,如何将每个文件名放入数组(或切片?),然后稍后在程序中访问每个值来操纵每个文件。 > strong> p>

它不需要是顺序的。 p>

这是工作代码(我想是从亚当·伍(Adam Ng)修改而来的) p>

 包main 
 
 import(
“  fmt“ 
” os“ 
”路径/文件路径“ 
” bufio“ 
 //”字节“ 
 //” io / ioutil“ 
 
)
 
 func main(){\  n 
 
 lineScan:= bufio.NewScanner(os.Stdin)
 var inputText字符串
 
 fmt.Print(“输入文件扩展名以搜索:.extension 
”)
 lineScan.Scan()  
 inputText = lineScan.Text()
 
目录名:=“。  +字符串(filepath.Separator)
 
d,err:= os.Open(dirname)
 if err!= nil {
 fmt.Println(err)
 os.Exit(1)
} 
 延迟d.Close()
 
个文件,err:= d.Readdir(-1)
如果err!= nil {
 fmt.Println(err)
 os.Exit(1)
} \  n_n代表_,文件:=范围文件{
如果file.Mode()。IsRegular(){
 
如果filepath.Ext(file.Name())== inputText {
 
 fmt。  Println(file.Name())
} 
} 
} 
} 
  code>  pre> 
  div>

I tweaked your code so that it will put each filename into a slice of strings and then print the slice at the end. Also, keep in mind that you already have a file list in the 'files' variable.

package main

import (
  "bufio"
  "fmt"
  "os"
  "path/filepath"
  //"bytes"
  //"io/ioutil"
)

func main() {

  lineScan := bufio.NewScanner(os.Stdin)
  var inputText string

  fmt.Print("Enter file extension to search for: .extension 
")
  lineScan.Scan()
  inputText = lineScan.Text()

  dirname := "." + string(filepath.Separator)

  d, err := os.Open(dirname)
  if err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
  defer d.Close()

  files, err := d.Readdir(-1)
  if err != nil {
    fmt.Println(err)
    os.Exit(1)
  }

  fileList := make([]string, 0)
  for _, file := range files { 
    if file.Mode().IsRegular() {

      if filepath.Ext(file.Name()) == inputText {

        fmt.Println(file.Name())
        fileList = append(fileList, file.Name())
      }
    }
  }

  fmt.Println("File List: ", fileList)
}

I hope this works for you.