10 examples of 'golang list files in directory' in Go

Every line of 'golang list files in directory' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
95func listDirectory(root, relpath string) (data []interface{}, err error) {
96 dir := filepath.Join(root, relpath)
97 file, err := os.Open(dir)
98 if err != nil {
99 return
100 }
101 defer file.Close()
102 files, err := file.Readdir(-1)
103 if err != nil {
104 return
105 }
106 data = make([]interface{}, 0, len(files))
107 for _, finfo := range files {
108 data = append(data, inspectFileInfo(root, relpath, finfo))
109 }
110 return
111}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

191func (store *s3Storage) ListDirectoryFiles(path string) ([]string, error) {
192 prefix := normalizePath(filepath.Join(store.directory, path))
193 if !strings.HasSuffix(prefix, "/") {
194 prefix += "/"
195 }
196 objOut, err := store.svc.ListObjects(&s3.ListObjectsInput{
197 Bucket: aws.String(store.bucketName),
198 Delimiter: aws.String("/"),
199 Prefix: aws.String(prefix),
200 })
201 if err != nil {
202 return nil, err
203 }
204 if *objOut.IsTruncated {
205 log.Println("Warning: S3 directory listing truncated. This will result in buggy behavior until pagination is implemented.")
206 }
207 resp := make([]string, 0, len(objOut.Contents))
208 for _, item := range objOut.Contents {
209 filename := strings.TrimPrefix(*item.Key, path)
210 resp = append(resp, filename)
211 }
212 return resp, nil
213}
65func getFilelist(path string)(pyfilelist [] string, err error) {
66 reterr := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
67 if ( f == nil ) {return nil}
68 if f.IsDir() {return nil}
69 if strings.Contains(path, ".py") == true{
70 pyfilelist = append(pyfilelist, path)
71 }
72 return nil
73 })
74
75 if reterr != nil {
76 fmt.Printf("filepath.Walk() returned %v\n", reterr)
77 os.Exit(1)
78 }
79 return pyfilelist, nil
80}
87func ListFilesOrDir(path string, listType string) ([]string, error) {
88 var pathSlice []string
89 err := filepath.Walk(path, func(path2 string, f os.FileInfo, err error) error {
90 if f == nil {
91 return err
92 }
93 if f.IsDir() {
94 if listType == "DIR" || listType == "ALL" {
95 pathSlice = append(pathSlice, path2)
96 }
97 } else if listType == "FILE" || listType == "ALL" {
98 pathSlice = append(pathSlice, path2)
99 }
100 return nil
101 })
102 return pathSlice, err
103}
137func (s *Discovery) List(directory string) ([]*store.KVPair, error) {
138 return s.store.List(directory)
139}
120func (c *Handler) dirList(w io.Writer, files []os.FileInfo) error {
121 for _, file := range files {
122 fmt.Fprintf(w, "%s\r\n", fileStat(file))
123 }
124 fmt.Fprint(w, "\r\n")
125 return nil
126}
107func list(context *config.Context, path string, hidden bool) (files []*File, err error) {
108 absPath := context.AbsPathOf(path)
109 var f []os.FileInfo
110 if f, err = ioutil.ReadDir(absPath); err != nil {
111 return
112 }
113 for _, file := range f {
114 if hidden || !strings.HasPrefix(file.Name(), ".") {
115 files = append(files, NewLocalFile(gopath.Join(absPath, file.Name()), file))
116 }
117 }
118 return
119}
45func (c *OutputConfig) ListFiles() ([]string, error) {
46 files := make([]string, 0, 10)
47
48 visit := func(path string, info os.FileInfo, err error) error {
49 if err != nil {
50 return err
51 }
52 if !info.IsDir() {
53 files = append(files, path)
54 }
55 return nil
56 }
57
58 return files, filepath.Walk(c.OutputDir, visit)
59}
141func List(dir, suffix string, isDir ...bool) (files []string, err error) {
142 files = make([]string, 0, 10)
143 dirIo, err := ioutil.ReadDir(dir)
144 if err != nil {
145 return nil, err
146 }
147
148 pthSep := string(os.PathSeparator)
149 suffix = strings.ToUpper(suffix)
150 for _, fi := range dirIo {
151 if len(isDir) > 0 {
152 if !fi.IsDir() {
153 continue
154 }
155 } else {
156 if fi.IsDir() {
157 continue
158 }
159 }
160
161 if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
162 files = append(files, dir+pthSep+fi.Name())
163 }
164 }
165
166 return files, nil
167}
62func (s *libKVStore) List(ctx context.Context, directory string) ([]*KVPair, error) {
63 pairs, err := s.store.List(directory)
64 if err != nil {
65 return nil, fromLibKVStoreErr(err)
66 }
67 kvPairs := make([]*KVPair, len(pairs))
68 for i, p := range pairs {
69 kvPairs[i] = &KVPair{Key: p.Key, Value: p.Value, LastIndex: p.LastIndex}
70 }
71 return kvPairs, nil
72}

Related snippets