Beginners guide to Go-Lang | Part2

  • 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.




Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Laravel authentication using Fortify: What is Laravel Fortify?

Technical Interview Prep: List of Depths — Ruby

Visualizing Song Trends on TikTok

鬼灭之刃剧场版:无限列车篇 2020 (Demon Slayer: Kimetsu no Yaiba) ZH▷ 完整版本 HD/BluRay

Automate TradingView Alerts Without Any Coding

Complete Infrastructure Automation on AWS with Terraform

Task — 19

Some notes from MIT’s ./missing-semester course: Lecture 1

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

More from Medium

Go-Lang Hands-On| Part-3

Learning Go from Documentation #2 (Call your code from another module)

Web Sockets in Golang

Consuming an API with Golang