> On 14 Jul 2016, at 05:56, nakuldesa...@gmail.com 
> <mailto:nakuldesa...@gmail.com> wrote:
> 
> // semaphore to control the number of open file descriptors
> var sem = make(chan struct{}, 20)
> 
> func dirents(dirName string) []os.FileInfo {
>    defer func() {
>        <-sem
>    }()
> 
>    // Acquire a semaphore to read a Dir
>    sem <- struct{}{}
> 
>     entrySlice, err := ioutil.ReadDir(dirName)
> 
>     if err != nil {
>         return nil
>     }
>     return entrySlice
> }
> 
> 
> func walkDir(directoryName string, filesizeChan chan int64, wg 
> *sync.WaitGroup) {
>      defer wg.Done()
> 
>      for _, entry := range dirents(name) {
>          if entry.IsDir() {
>               // if it is a directory, recurse
>               subDirName := ...
>               wg.Add(1)
>               go walkDir(...)
>           } else {
>               // if it is a file, send down the size
>               filesizeChan <- entry.size
>          }
>     }
> }

> […]

> func walkDir(directoryName string, filesizeChan chan int64, wg 
> *sync.WaitGroup) {
>     defer wg.Done()
> 
>     defer func() {
>          <-sem
>     }()
> 
>     // Acquire a semaphore be proceeding
>     sem <- struct{}{}
> 
>     for _, entry := range dirents(name) {
>          if entry.IsDir() {
>               // if it is a directory, recurse
>               subDirName := ...
>               wg.Add(1)
>               go walkDir(...)
>           } else {
>               // if it is a file, send down the size
>               filesizeChan <- entry.size
>          }
>     }
> }

Are these two approaches not semantically identical? The former call dirents() 
and aquire semaphore then do the work. The latter aquires semaphore then calls 
dirents() then do the work. If you remove the function call to dirents and 
inline the code it’s identical.

Have I missed something?

Jason

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to