Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing directories in go routines

Tags:

go

I am trying to change directories in a go routine to directory x. I now want to use a different go routine that changes the directory to directory y. Will the execution of my first go routine be affected by this change to the current working directory in the second go routine? The purpose of wanting to do this is to introduce parallelism while doing similar tasks. If it does end up changing the CWD, what should be an alternative approach (forking...)?

like image 800
Rohan Nahata Avatar asked Jan 18 '26 03:01

Rohan Nahata


1 Answers

Like mentioned in the comments, keeping track of the current working directory in each goroutine will cause problems.

Try using filepath.Abs to capture the absolute directory and store that instead. Then each goroutine can operate on it's own directory without worrying about it being "switched" under the hood. Just be sure you're not modifying the same file accidentally by multiple goroutines.

Edit: Removing a chunk of text per @Evan's comment. Use absolute paths :p

like image 133
Jeffrey Martinez Avatar answered Jan 19 '26 18:01

Jeffrey Martinez