Skip to content

Throws an error when trying to open file inside goroutine #1

@ghost

Description

Thanks for this nice little module. I am having trouble when trying to run this code with a go subroutine.

The below code works perfectly without go subroutine in the anonymous function inside for loop. But fails with error http request error: <URL> connect: can't assign requested address hen tried with go subroutine.

func main() {
	req, _ := http.NewRequest("GET", "https://dl.google.com/go/go1.10.windows-amd64.zip", nil)

	htrdr, err := httpreaderat.New(nil, req, nil)
	if err != nil {
		panic(err)
	}
	bhtrdr := bufra.NewBufReaderAt(htrdr, 1024*1024)

	zrdr, err := zip.NewReader(bhtrdr, htrdr.Size())
	if err != nil {
		panic(err)
	}

       	var wg sync.WaitGroup
	for _, f := range zrdr.File {
                fileToCopy := f
		go func(fileToCopy *zip.File) {
                    fr, err := f.Open()
		    check(err)
		    fileloc := f.FileInfo().Name()))
                     
                     outFile, err := os.Create(fileloc)
		    check(err)
		    defer outFile.Close()
				
                     _, err = io.Copy(outFile, fr)
		    if err != nil {
		        log.Fatalf("Error: %v", err)
		    }
		    fr.Close()
		    wg.Done()
                  }(fileToCopy)
	}
       wg.Wait()
}

Have I missed the mark anywhere

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions