Skip to main content
edited tags
Link
G. Sliepen
  • 61.7k
  • 3
  • 61
  • 152
Minor copy-edits
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

I'm trying to write a simple program to find a certain file   name within a directory tree. I use multiple coroutines to find (upup to 30 coroutines).

Is there anything wrong with this code or what needs to be improved?

import (
    "fmt"
    "io/ioutil"
    "path/filepath"
    "sync"
)

const tofind = "select-test.go"

func main() {
    wg := sync.WaitGroup{}
    search("/Users/user/Documents", &wg)
    wg.Add(1)
    wg.Wait()
}

var permit = make(chan struct{}, 30)

func search(dir string, wg *sync.WaitGroup) {
    permit <- struct{}{}

    defer func() {
        <-permit
    }()
    defer func() {
        wg.Done()
    }()

    items, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err.Error())
    }
    for _, item := range items {
        if item.IsDir() {
            wg.Add(1)
            go search(filepath.Join(dir, item.Name()), wg)
        } else {
            if item.Name() == tofind {
                fmt.Println(filepath.Join(dir, item.Name()))
            }
        }
    }
}

I'm trying to write a simple program to find a certain file  . I use multiple coroutines to find (up to 30 coroutines)

Is there anything wrong with this code or what needs to be improved?

import (
    "fmt"
    "io/ioutil"
    "path/filepath"
    "sync"
)

const tofind = "select-test.go"

func main() {
    wg := sync.WaitGroup{}
    search("/Users/user/Documents", &wg)
    wg.Add(1)
    wg.Wait()
}

var permit = make(chan struct{}, 30)

func search(dir string, wg *sync.WaitGroup) {
    permit <- struct{}{}

    defer func() {
        <-permit
    }()
    defer func() {
        wg.Done()
    }()

    items, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err.Error())
    }
    for _, item := range items {
        if item.IsDir() {
            wg.Add(1)
            go search(filepath.Join(dir, item.Name()), wg)
        } else {
            if item.Name() == tofind {
                fmt.Println(filepath.Join(dir, item.Name()))
            }
        }
    }
}

I'm trying to write a simple program to find a certain file name within a directory tree. I use up to 30 coroutines.

Is there anything wrong with this code or what needs to be improved?

import (
    "fmt"
    "io/ioutil"
    "path/filepath"
    "sync"
)

const tofind = "select-test.go"

func main() {
    wg := sync.WaitGroup{}
    search("/Users/user/Documents", &wg)
    wg.Add(1)
    wg.Wait()
}

var permit = make(chan struct{}, 30)

func search(dir string, wg *sync.WaitGroup) {
    permit <- struct{}{}

    defer func() {
        <-permit
    }()
    defer func() {
        wg.Done()
    }()

    items, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err.Error())
    }
    for _, item := range items {
        if item.IsDir() {
            wg.Add(1)
            go search(filepath.Join(dir, item.Name()), wg)
        } else {
            if item.Name() == tofind {
                fmt.Println(filepath.Join(dir, item.Name()))
            }
        }
    }
}
added 2 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

I'm trying to write a simple program to find a certain file . I use multiple coroutines to find (up to 30 coroutines)

Is there anything wrong with this code or what needs to be improved?

import (
    "fmt"
    "io/ioutil"
    "path/filepath"
    "sync"
)

const tofind = "select-test.go"

func main() {
    wg := sync.WaitGroup{}
    search("/Users/user/Documents", &wg)
    wg.Add(1)
    wg.Wait()
}

var permit = make(chan struct{}, 30)

func search(dir string, wg *sync.WaitGroup) {
    permit <- struct{}{}

    defer func() {
        <-permit
    }()
    defer func() {
        wg.Done()
    }()

    items, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err.Error())
    }
    for _, item := range items {
        if item.IsDir() {
            wg.Add(1)
            go search(filepath.Join(dir, item.Name()), wg)
        } else {
            if item.Name() == tofind {
                fmt.Println(filepath.Join(dir, item.Name()))
            }
        }
    }
}
```

I'm trying to write a simple program to find a certain file . I use multiple coroutines to find (up to 30 coroutines)

Is there anything wrong with this code or what needs to be improved?

import (
    "fmt"
    "io/ioutil"
    "path/filepath"
    "sync"
)

const tofind = "select-test.go"

func main() {
    wg := sync.WaitGroup{}
    search("/Users/user/Documents", &wg)
    wg.Add(1)
    wg.Wait()
}

var permit = make(chan struct{}, 30)

func search(dir string, wg *sync.WaitGroup) {
    permit <- struct{}{}

    defer func() {
        <-permit
    }()
    defer func() {
        wg.Done()
    }()

    items, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err.Error())
    }
    for _, item := range items {
        if item.IsDir() {
            wg.Add(1)
            go search(filepath.Join(dir, item.Name()), wg)
        } else {
            if item.Name() == tofind {
                fmt.Println(filepath.Join(dir, item.Name()))
            }
        }
    }
}
```

I'm trying to write a simple program to find a certain file . I use multiple coroutines to find (up to 30 coroutines)

Is there anything wrong with this code or what needs to be improved?

import (
    "fmt"
    "io/ioutil"
    "path/filepath"
    "sync"
)

const tofind = "select-test.go"

func main() {
    wg := sync.WaitGroup{}
    search("/Users/user/Documents", &wg)
    wg.Add(1)
    wg.Wait()
}

var permit = make(chan struct{}, 30)

func search(dir string, wg *sync.WaitGroup) {
    permit <- struct{}{}

    defer func() {
        <-permit
    }()
    defer func() {
        wg.Done()
    }()

    items, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err.Error())
    }
    for _, item := range items {
        if item.IsDir() {
            wg.Add(1)
            go search(filepath.Join(dir, item.Name()), wg)
        } else {
            if item.Name() == tofind {
                fmt.Println(filepath.Join(dir, item.Name()))
            }
        }
    }
}
Source Link
Loading