World Book Day: Bringing you closer to Go programming thinking

Permalink to this article – https://ift.tt/GLDTkd1

After more than ten years of evolution and development, the Go language has had millions of fans around the world. Among these developers, in addition to some new programming language beginners, more Developers turned from the programming language camp. Since the Go language is easy to use, everyone quickly mastered the Go syntax in the early days of switching to Go.

But after writing more Go code, many people find themselves writing Go code that always feels awkward, and always try to find syntax elements in Go that are familiar to them from their last language. My own Go code style seems to be quite different from the Go standard library and the code of mainstream Go open source projects in terms of thinking and usage, and whenever I see the code of the Go core development team, I always feel empowered. The main reason for this is that the way of thinking of the last programming language in the brain is “playing”.

This article will take a detailed look at the relationship between programming language and programming thinking and what the programming thinking of Go language is through the content of the book “The Road to Go Language Improvement: Programming Ideas, Methods and Skills from Novices to Experts” . Help everyone understand Go programming more deeply.

Before understanding Go programming thinking, let’s take a look at what is the connection between thinking and language?

1. Language and thinking – from the master’s point of view

There is a well-known hypothesis in the field of human natural linguistics-“Sapir-Wulf hypothesis”, the content of this hypothesis is as follows: ” Language affects or determines the way of thinking of human beings .”

Speaking of this hypothesis, we cannot fail to mention that in early 2017, a well-reputed American sci-fi blockbuster “Arrival” was released in China. This film was adapted from Hugo Award-winning Chinese sci-fi novelist Ted Jiang’s “Your Life” story”. The theoretical basis of the main plot in the film is the “Sapir-Worf hypothesis”. What is even more exaggerated is that the hypothesis is directly applied to the alien language in the film, extending it to the universe. The heroine in the film communicates with aliens as a representative of human beings, and learns alien languages. Since then, her thinking has changed greatly, and she has the “super power” to predict the future. This is also the ultimate expression of language affecting thinking.

The amazing thing is that in the programming language world, there is a master figure who also has the same viewpoint and cognition as the “Sapir-Worf hypothesis”. He is the first Turing Award winner and famous computer scientist Alan Paley ( Alan J. Perlis), who put it from another angle: “A programming language that does not affect the way you think about programming is not worth learning and using.”

2. “Projection” in reality

From the theories and viewpoints of the above-mentioned masters, we can see that there is a certain connection between language and thinking. So what does the projection of this connection between the two look like in the real programming world? Let’s look at a simple programming problem – prime number sieve:

  • Problem description: A prime number is a natural number that has two distinct natural number divisors: 1 and itself. The problem here is how to find prime numbers less than or equal to a given integer n. To solve this problem, we can use the Sieve of Eratosthenes algorithm.

  • Algorithm description: first use the smallest prime number 2 to sieve, and remove the multiples of 2; the next unfiltered number is the prime number (here is 3). Then use this prime number 3 to sieve, and sieve to remove multiples of 3… This is repeated until the sieve is complete (see Figure 1 for the algorithm diagram).

Figure 1. Diagram of prime number sieve algorithm

Below are the implementation versions of this prime sieve algorithm in different programming languages.

(1) C language version

 // sieve.c #include <stdio.h> #define LIMIT 50 #define PRIMES 10 void sieve() { int c, i,j,numbers[LIMIT], primes[PRIMES]; for (i=0;i<LIMIT;i++){ numbers[i]=i+2; /*fill the array with natural numbers*/ } for (i=0;i<LIMIT;i++){ if (numbers[i]!=-1){ for (j=2*numbers[i]-2;j<LIMIT;j+=numbers[i]) numbers[j]=-1; /* 筛除非素数*/ } } c = j = 0; for (i=0;i<LIMIT&&j<PRIMES;i++) { if (numbers[i]!=-1) { primes[j++] = numbers[i]; /*transfer the primes to their own array*/ c++; } } for (i=0;i<c;i++) printf("%d\n",primes[i]); }

(2) Haskell version

 // sieve.hs sieve [] = [] sieve (x:xs) = x : sieve (filter (\a -> not $ a `mod` x == 0) xs) n = 100 main = print $ sieve [2..n]

(3) Go language version

 // sieve.go func Generate(ch chan<- int) { for i := 2; ; i++ { ch <- i } } func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in if i%prime != 0 { out <- i } } } func main() { ch := make(chan int) go Generate(ch) for i := 0; i < 10; i++ { prime := <-ch print(prime, "\n") ch1 := make(chan int) go Filter(ch, ch1, prime) ch = ch1 } }

Comparing the implementation of the prime number sieve algorithm in the above three language versions, we see:

  • The C version of the prime sieve program is a regular implementation. It defines two arrays: numbers and primes. The “sieve” process is performed in the numbers array (that is, based on pure memory modification), and the non-prime array elements are set to -1, which is convenient for subsequent extraction;

  • The Haskell version adopts the idea of ​​function recursion. Through the “filter operation set”, use the predicate (filter condition) \a -> not $ a mod x == 0; filter out the multiples of prime numbers, and use the set of unfiltered numbers as The parameters are passed recursively to the next;

  • The Go version program implements a concurrent prime number sieve, which uses a concurrent combination of goroutines. The program starts from the prime number 2, and establishes a goroutine for each prime number in turn, which is used to filter out the multiples of the prime number. ch points to the source channel of the sieve goroutine where the latest output prime number is located. This code comes from a sharing on concurrency by Rob Pike. The execution process of the Go version program can be shown in Figure 2 three-dimensionally.

Figure 2 Execution diagram of prime number sieve in Go version

3. Go language native programming thinking

Through the above-mentioned real-life problem, we can see that when faced with the same problem, programmers from different programming languages ​​have given different solutions to their thinking styles: C’s imperative thinking, Haskell’s functional thinking, and Go’s Concurrent thinking. Combined with the “Sapir-Wolf Hypothesis”, we can get an inference that is not theoretically confirmed but does have an impact on reality: programming languages ​​affect programming thinking, or each programming language has its own native programming thinking .

The Go language was born late, and most Gophers (including the author) are not the first language of Go, they are all “halfway monks” transferred from other languages, such as C, C++, Java, Python, etc. Each language has its own native programming mindset. For example: C language believes in programmers and provides pointers and pointer operations, allowing C programmers to play freely, and direct memory operations close to the underlying layer make C programs have high performance; C++ supports multi-paradigm (imperative, OO and generics) ), although it does not force programmers to use a specific paradigm, it is recommended to use the latest advanced paradigms such as generics that represent the characteristics of modern language development; Python language has formed Pythonic rules to guide Python programmers to write Python thinking or idiomatic law code.

Experience tells us that any code that belongs to the high-quality category of a programming language must be code written under the native thinking of this programming language. If you use the thinking of language A to write the code of language B (such as writing C code with OO thinking, writing Haskell code with imperative thinking, etc.), then most of the code you write will not be recognized by the language B community, and it will be more difficult to become a An example of high-quality code. Moreover, if you learn and practice the B language in this direction, the result can only be “different directions”, and the goal of writing high-quality code is getting farther and farther away.

So what exactly is Go native programming thinking? The programming thinking of a programming language is a unified thinking habit, behavior, code idiom and style formed by language designers, language implementation teams, language communities, and language users in the long-term evolution and practice. It has been more than ten years since the Go language was born. After being nurtured by the Go design philosophy, guided and educated by the Go development team, and practiced by the Go community, the Go language has gradually formed its own native programming thinking, or the idiomatic go that conforms to the Go language philosophy. They are the essence of the Go language, and the skeleton that builds the content of this book, and deserves to be presented in detail at the scale of a book. Therefore, it can be said that the process of reading this book is also the process of learning and establishing the native programming thinking of Go language.

4. Summary

This article details the connection between programming languages ​​and programming thinking. When we learn and use a programming language, the goal is to write high-quality code using the language’s native way of thinking. To learn Go, you must use Go’s native programming thinking to write Go code, instead of thinking in other languages. Mastering Go native programming thinking is our learning direction and the only way to high-quality Go programming. If you want to learn more about Go programming thinking, I recommend you to read my new book “The Road to Go Language Improvement: Programming Ideas, Methods and Skills from Novices to Experts”.


Gopher Daily Archive Repository – https://ift.tt/0RKBfZM

my contact information:

  • Weibo: https://ift.tt/TRjXEAO
  • WeChat public account: iamtonybai
  • Blog: tonybai.com
  • github: https://ift.tt/epbiRgy
  • “Gopher Tribe” Planet of Knowledge: https://ift.tt/YgA0ieR

Business cooperation methods: writing, publishing books, training, online courses, partnership entrepreneurship, consulting, advertising cooperation.

© 2022, bigwhite . All rights reserved.

This article is reprinted from https://tonybai.com/2022/04/23/taking-a-closer-look-at-programming-thinking-in-go/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment