0

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 \n")     
    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())
              }
          }
      }
  }
2
  • 3
    Go uses append to append to a slice. Is that what you're looking for? Commented Jan 11, 2017 at 18:27
  • This question, which is actually basic Go, would benefit from a more concise code black that consists of only your attempt to append to a slice in a loop. The answer could then be something much simpler. The details about opening files are unrelated to the question as stated. Commented Apr 27, 2018 at 16:04

1 Answer 1

3

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 \n")
  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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a idea here of what you've done. I assume now that I can work with fileList in a while or a for each loop to pull each string out and do something with them. I would like a reference (for dummies like me) document or an example if you would be so kind.
@ArkhamAngel: The best reference is the documentation on appending slices, or even the append section of Effective Go. I would encourage you to go through the Tour Of Go, which cover all these basic Go concepts.
Here is an additional resource on appending to a slice for your reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.