dongwei6700 2017-01-11 18:25
浏览 623
已采纳

在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())
              }
          }
      }
  }
  • 写回答

1条回答 默认 最新

  • doupiao9318 2017-01-11 18:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料