image
Errors
Start course
Difficulty
Beginner
Duration
2h 17m
Students
3768
Ratings
4.6/5
starstarstarstarstar-half
Description

If you're thinking about engineering the next big dotcom application then you should seriously consider using Go!! 

The Go Programming Language is without doubt one of the hottest languages to learn, particularly in this cloud native era. More and more companies are adopting Go to engineer highly performant, stable and maintainable applications. Popular projects such as Docker, Kubernetes, Terraform, Etcd, Istio, InfluxDB have all been built successfully using Go!! 

This introductory level training course is designed to bring you quickly up to speed with the many key features that the Go programming language provides. You'll also learn how to setup your own Go development environment - consisting of the Go toolchain, Visual Studio Code, and several related Go based extensions - all to ensure that you are able to be productive writing your own source code.

We’d love to get your feedback on this course, so please give it a rating when you’re finished. If you have any queries or suggestions, please contact us at support@cloudacademy.com.

Learning Objectives

By completing this course, you will:

  • Learn about what makes Go a great language
  • Learn how to install the Go toolchain
  • Learn how to setup Visual Studio Code to edit and debug Go programs
  • Learn how to work with the Go Playground to test and run snippets of Go code
  • Learn and understand the basic Go language syntax and features
  • Learn how to use the Go tool chain commands to compile, test, and manage Go code
  • And finally, you’ll learn how to work with and manage Go modules for module dependency management

Intended Audience

This course is intended for:

  • Anyone interested in learning the Go Programming Language
  • Software Developers interested in using Go to compile and test Go based applications
  • DevOps practitioners looking to learn about Go to support Go based applications

Prerequisites

To get the most from this course, you should have at least:

  • A basic understanding of software development and the software development life cycle

Source Code

All sample Go source code as used and demonstrated within this course can be found here:

Transcript

- [Jeremy Cook] When building large and complex Go programs it's always difficult to anticipate all of the possible outcomes that can occur when the program runs. Error conditions exist and should be handled gracefully. Go provides an ability to raise and communicate errors through the built in error interface type which is managed via several useful functions found within the errors package. 

The mechanism in which errors are communicated back up the call chain happens to be via a functions capability of returning multiple values, one of which will be the error itself. Back within the callee scope - error conditions can then be checked upon and actions taken accordingly using the same language features already reviewed. In most cases checks will be done using a simple if/else statement that tests to see if the returned error variable is nil or not. In the example provided here - we begin by importing the "errors" package on line 4. Next the circleArea function is declared on lines 7 to 13 - and for which performs a simple validation check on the incoming radius parameter to ensure that its value is positive. 

The function signature has a multi return value - composed inside the last set of parentheses and consisting of the area float32 and an error which is of type error. In the case that the radius parameter is not a positive value - an error is created and returned back to the caller - this can be seen on line 9. In the case that the functions execution runs without errors, meaning the radius is a positive number then the error return value is set to nil as can be seen on line 11. Within the main function on line 16, the circleArea function is called with the radius set to 3. In this case since I have knowledge that the circleArea function will execute without causing any errors. 

I can simply ignore the returned error value by assigning it to an underscore. Lines 19 to 23 however, demonstrates an invocation of the circleArea function with a negative radius - in this case -3. The invocation of the circleArea function is done inline within an if/else statement. An explicit test is done against the returned error variable to test whether it contains an error or not. Not being the case when it is 6 nil. Let's run this example and examine the resulting output. Here we can see that the first call to the circleArea function succeeded with the result 28.27 and that the second call to the circleArea function failed with the error message "radius should be positive" message.

In summary, you've just observed How to import the errors package to give you the ability to create errors How to return errors using the errors .New function And how to test for returned errors and react accordingly

About the Author
Students
133196
Labs
68
Courses
111
Learning Paths
189

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, Azure, GCP), Security, Kubernetes, and Machine Learning.

Jeremy holds professional certifications for AWS, Azure, GCP, Terraform, Kubernetes (CKA, CKAD, CKS).

Covered Topics