📜 ⬆️ ⬇️

Go main advantage

If you ask the average Go-developer, what are the benefits of Go - most likely you will hear the familiar list of buns. Much has already been written about them, but very often another thing costs, a much more interesting one is the long-term effect of certain language design decisions. I want to reveal this topic a little wider, which is actually relevant not only for Go. But in this article I will take two aspects for example - the way that errors are handled in Go and the testing system and I will try to show how the design of a language forces people to write better code.



Error processing


You probably know that in imperative languages ​​there are two main mechanisms for reporting an error — throw an exception or return a code / value explicitly. One could say that there are even two camps here - supporters of exceptions and supporters of an obvious return, but everything is somewhat worse. There are actually two camps, but they are different - those who understand the importance of error handling in code, and those who for the most part ignore this aspect of programming. And the second camp is the undisputed leader so far.

It is logical to assume that this is exactly what distinguishes "good" programmers from "bad" programmers, and there is certainly some truth in this. But there is one thing. The toolkit - in this case, this “programming language” - also solves. If your language allows you to do “wrong” a lot easier than do “right” - rest assured, no amount of articles and books “How not to write on [LANG]” will help - people will continue to do wrong. Just because it is easier.
')
It would seem that every schoolchild already knows that “global variables” are evil. How many articles on this topic - everything seems to be all clear. But nevertheless - even now, in 2015, you will find tons of code using global variables. Why?
But because creating a global variable - “do wrong” takes exactly one line in almost any programming language. At the same time, in order to create any of the “right options”, any minimal wrapper, you need to spend more time and effort. Even if it is 1 character more - but it solves.
This is very important to realize - the toolkit decides. The toolkit forms our choice.

But let us go back to error handling and try to understand why the authors of Go found exceptions - “the wrong way”, decided not to implement them in Go, and what is the difference between “returning several values” in Go from similar ones in other languages.

Take for example a simple thing - opening a file.
Here is the C ++ code
ifstream file;
file.open ("test.txt");

, «» failbit , ifstream.exeptions() try{} catch{} . «» — , « ».

Python:
file = open('test.txt', 'r')

— open(), . « » « try-catch, --».

( — , C++ Python — , , . «» .)

Go:
file, err := os.Open("test.txt")

— , «» . error , — Go:
./main.go:8: err declared and not used

, _, - , :
if err != nil {
	log.Fatal("Aborting: ", err)
}


«» Go — . , « » — , strconv.Atoi() — — , — , . - .

. — . , .


- , — «» , — . , . «» — ~100% , «» — . — , , .

Go — : . — mycode_test.go , Test:
import "testing"
func TestMycode(t *testing.T) {
}

. — if-. , .

,
go test

. -cover, .

game-changer. , « », , « » . , . Go .

, , , , , , , , — . , QA-. «», «» .

. , , — , — . Go — « ». TDD — .


— «» , «». Go KISS- « » ( ) . -, Go .

, , Go.


Why Go gets exceptions right
It's 2015. Why do we still write insecure software?

Source: https://habr.com/ru/post/248857/


All Articles