1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. Introduction to the Go Programming Language

Type Assertions

play-arrow
Start course
Overview
DifficultyBeginner
Duration2h 17m
Students810
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] Go provides you with the ability to perform type assertions on interface types. A type assertion provides access to an interface's underlying concrete type. 

The type assertion "x. " asserts that the concrete value stored in x is of type T, and that x is not nil. In the example provided here, a custom company struct type is declared on lines 5 to 7. Then, within the main function on line 10, the variable x is declared using the empty interface type and is then assigned an instance of the company struct type. 

On line 12, short notation is used to cast x to its concrete type and assign the cast to the variable c1, which is then immediately printed out on line 13. Lines 15 to 17 demonstrate how to do the exact same cast but in a way that allows the application to keep running should the cast fail. Here the type assertion is checked to see if the assertion was true or false. If you don't use this technique to test your assumed type assertion and it turns out to be incorrect, a runtime panic is thrown and the program could terminate if the panic isn't recovered from, as is the case on line 19. Let's run this example as is, and as expected the program completes without any panics being encountered. 

Next let's uncomment line 19. Now this time this should result in a panic, since this particular cast will not work. Rerunning the program indeed results in the expected crash. 

In summary you've observed how to implement type assertions, how to test type assertions, allowing the application to have alternate path flows depending on whether the type assertion was correct or not, and how unhandled and incorrect type assertions cause runtime panics resulting in possible program termination.

About the Author
Students36187
Labs33
Courses93
Learning paths23

Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.

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

Jeremy holds professional certifications for both the AWS and GCP cloud platforms.
 

Covered Topics