• Error Handling while dealing with Files in Go.
  • Beautifying the error stack-trace.
  • How to signal errors in Go.
  • Demonstrating the use of Panic with examples in Go.
  • Recovering from errors in Go.
  • Go-Routines in Go.
  • Sequential code-flow in Go.
  • Building concurrency with GoRoutines.
  • Demonstrating WaitGroup in Go.
  • Communication between GoRoutines using Channels.
  • We got an error in output as shown above. Also we got exitCode of 1, which is good.
  • But we’re missing the context of why you wanted to open the file and where it happened. In languages, such as Python or Java, you’d use exceptions which will show you the stack trace where the problem was. To help us with this, Dave Cheney wrote a package called pkg/errors.
  • At line #16, we have a “recover” function which is an anonymous function, being executed in defer mode. This function would help us to recover safely from the panic situation.
  • Note that, as soon as the business of function “safeValue()” is finished, the anonymous recover function, shall be executed.
  • Observe that, post line #9, statements are executed perfectly fine and the flow of the program is not being disturbed.
  • Reading a file and return, in case file can’t be opened.
  • Next, we remove this file, that we just read.
  • Next, we convert the data from string format to Integer format using “Atoi” function.
  • We are going to use the Go keyword, to generate a go routines, and create an anonymous function.
  • The go run time will NOT wait for go routines that are still working. You can do a sleep to wait for them, but this is a very bad form of synchronisation. The same is reflecting well, in the output as well. No output is getting printed here.
  • Go has the sync package with a bunch of utilities synchronised between go routines, but the recommended way is to use channels.
  • You can use WaitGroup, which is exactly for waiting for a bunch of go routines to finish.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems