Error Handling on Golang

Before learning the Go language, I believed that Python was the most convenient language for providing error and exception handling for programmers; after learning the Go language, my opinion has not changed at all…

@DGideas

This article references @ethancai’s article and @davecheney‘s keynote speech at Gocon Spring 2016, as well as several blog posts and documents on golang.org.

The Go language offers a unique error handling experience compared to other programming languages. One of the distinctive features of the Go programming language is the frequent occurrence of the if err != nil statement block. Thanks to the multiple return values feature of Go functions, a function that may not always execute successfully can use an error type return value to indicate whether its execution process is in an abnormal state, for example:

f, err := os.Open("filename.ext")
if err != nil {
	log.Fatal(err)
}

Why we have-pointer and reference types?

… Obviously, we can refer to an object by name, but in C++ (most) objects ‘‘have identity.’’ That is, they reside at a specific address in memory, and an object can be accessed if you know its address and its type …

Bjarne Stroustrup 《The C++ Programming Language》(Fourth Edition),Chapter 7.

During our last discussion, @Lollipop9z(Invalid Link)raised this interesting question. Due to their background in learning Python programming language, they were puzzled about why features like pointers and references are provided in languages like C++. In fact, many students who have studied programming languages that include concepts of pointers and/or references still lack a thorough understanding of the reasons behind the existence of these language elements. The following code snippet illustrates this using C++ as an example.