V
V
Vadim Rublev2021-08-21 01:42:33
go
Vadim Rublev, 2021-08-21 01:42:33

How to search for files in only one folder; specify the current directory?

var files []string
var my_dir = "../testDir/"
var err = filepath.Walk(my_dir, func(path string, info os.FileInfo, err error) (error) {
    if !info.IsDir() {
        var ext = filepath.Ext(path)
        if ext == ".tx" {
            files = append(files, path)
        }
    }
    return nil
})

Iterates over the specified folder and all subfolders. How to make it work only with the specified one?
And how to specify the current folder if you cannot specify a name (for example, the root folder of the site on the server)? "Current" is the folder where the application is running.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-08-21
@Vadim Rublev

To specify the current folder - you can specify "dot" as the folder name so that there is no recursion - use the ioutil.ReadDir function.

package main

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

func main() {
    // текущая папка
    files, err := ioutil.ReadDir(".")
    if err != nil {
        log.Fatal(err)
    }

    for _, file := range files {
        fmt.Println(file.Name(), file.IsDir())
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question