The beginning of GO: Entering the world of Golang with Java in hand

1. Introduction to Golang

Go (also known as Golang) is a high-level programming language for computers developed by Google's Robert Griesemer, Rob Pike and Ken Thompson.

Go's official documentation: https://golang.org

Go's Chinese api documentation: https://studygolang.com/pkgdoc

Go Chinese community website: https://studygolang.com/

1. The birth of Go

Before the launch of go, there were already many high-level programming languages, such as: C, C++, C#, Java... . Why make another GO?

There are three main reasons:

1> The computer hardware technology is updated frequently, and the performance improves rapidly. However, the development of mainstream programming languages ​​is obviously lagging behind the hardware, and the advantages of multi-core and multi-CPU cannot be reasonably used to improve the performance of software systems. Old programming languages ​​cannot strike a good balance between development efficiency and execution efficiency:

  • C++ and the like have high execution efficiency, low development efficiency, and slow compilation speed.
  • JAVA and the like have high compilation speed, but low execution efficiency.

Overall, the execution efficiency and development efficiency of each language:

  • Execution efficiency (execution speed): C/C++ > Java > Python
  • Developing efficiency: Python > Java > C/C++

On the one hand, the birth of the Go language is to have efficient execution speed, compilation speed and development speed at the same time.

2> The complexity of the software system is getting higher and higher, the maintenance cost is getting higher and higher, and there is a lack of a sufficiently concise and efficient programming language.

  • Within Google, as the scale of the project continues to expand, the complexity of development and maintenance is dragging Google into the quagmire step by step.
  • The system is becoming more and more complex, and a large amount of code is written in C++, Java, and Python, and the scale is getting larger and larger. A large number of programmers are required to work around tens of thousands of lines of code and millions of servers every day to ensure the normal operation of various services.

3> The enterprise operates and maintains a lot of c/c++ projects. Although the c/c++ program runs very fast, the compilation speed is really slow. At the same time, there are still a series of problems of memory leaks that need to be solved.

In response to the above problems, three Google engineers had a discussion on the afternoon of September 20, 2007, and it was this discussion that led to a three-person meeting organized the next day in Building 43, Mountain View, California. The three people present at the meeting were the founders of Go: Robert Griesemer, Rob Pike and Ken Thompson. The main achievements of these three gods are as follows:

  1. Robert Griesemer (one of the developers of Java HotSpot virtual machine and V8 engine)
  2. Rob Pike Rob Pike (Member of Bell Labs UNIX team, has participated in projects such as Plan9, Inferno and Limbo)
  3. Ken Thompson Ken Thompson (Member of the UNIX team at Bell Labs, one of the founders of C language, UNIX and Plan 9, and co-developed the UTF-8 character set specification with Rob Pike)

On Go's official website ( https://go.dev/solutions/google/ ), there is a passage from Rob Pike, which describes the birth, release, and current status of Go (2020):

insert image description here

2. Go's official website domain name

Why is it officially called Golang, but everyone is saying it is Go?

What's interesting is that when Google registered the domain name of Go's official website, go.orgthe domain name had already been registered by Disney. Instead: golang.orgthe domain name is used. This has also led many people to mistakenly think that Golang is its official name; however, we can only think that Golang is a nickname for Go.

3. Development of Go

The Go language originated in September 2007. In November 2009, Google officially released the Go language, and it was completely open sourced under the BSD protocol. In March 2012, go1.0 was released, and in August 2015, go1.5 was released (historic node , completely remove the C language part, use GO to compile GO). The latest version is go1.20. All release nodes ( transfer links ) are as follows:
insert image description here

In addition, during the version iteration process of the GO language, the language features have basically not changed much, and are basically maintained on the GO1.1 benchmark; and the official promise is that the new version is fully compatible with the code developed under the old version.

In the past 2-3 years, the ranking of GO language on the language ranking list ( https://www.tiobe.com/tiobe-index/ ) is relatively fixed (around 12th):
insert image description here

The overall language usage trends are as follows:

insert image description here

4. The design idea of ​​Go

The core design of GO is very simple for the sake of simplicity: "keep it simple", which pursues a simple and practical philosophy, reducing the complexity of code from the language level.

For example: implicit interface & slicing, weakening of classes, weakening of pointers, concise and efficient development.

5. Features of Go

Compared with other grammars, GO has the biggest feature: concurrency.

It natively supports concurrent programming. The Go language has extremely high support for network communication, concurrent and parallel programming, so that it can better utilize a large number of distributed and multi-core computers.

Developers can achieve this goal through the concept of goroutine, a lightweight thread, and then use channels to realize communication between goroutines. They automate segmented stack growth and goroutine multiplexing on a thread basis.

Subsequent extensions to GO's programming model (GMP).

However, GO also has many object-oriented language features that are not supported:

  • Function overloading and operator overloading are not supported, in order to simplify the design;
  • Does not support implicit conversion, in order to avoid some bugs and confusion in C/C++ development;
  • Abandon the inheritance of classes and types, and implement object-oriented design in other ways;
  • Generics are not supported;
  • Does not support dynamic loading code, dynamic link library;
  • Dynamic proxies are not supported;
  • Static variables are not supported;
  • Assertions are not supported;
  • The exception mechanism is replaced by recover and panic.

6. Go performance

Like C and C++, the Go language is also a compiled language. It directly compiles human-readable code into a binary file that the processor can run directly, with higher execution efficiency and better performance.

According to the Go development team and the basic algorithm test, the statistical results before March 28, 2013 show that the performance gap between Go language and C language is about 10%~20%.

A conservative estimate is that a Go program is 2x faster than a Java or Scala application and uses 70% less memory than either language, given the same environment and execution goals.

7. Go's mascot

Chipmunk (Gordon)

insert image description here

Second, the macro comparison of Go and Java

1> Cross-platform?

  • Because Java is supported by the JVM virtual machine, it has the feature of "compile once, run everywhere".
  • However, GO does not have a virtual machine and does not cross platforms (multiple operating systems). If you want to run on multiple platforms, you need to create different binary packages for each platform and compile them into executable programs corresponding to the server operating system version, such as: The exe program of windows.
    • What we call go cross-platform refers to the cross-platform between 32-bit and 64-bit operating systems.

2> Code style?

  • The Java language programming style is very object-oriented and provides many object-oriented features, such as: rewriting, overloading, inheritance, generics, dynamic proxy...
  • Go, based on its design principle **"keep it simple"**, is more concise and can achieve the same function with less code.

3> Concurrency performance (underlying CPU utilization)?

  • Before the GO1.5 version, the bottom layer was also implemented in C just like Java.
  • When JAVA was born (1995), there were no computers with multi-core CPUs at the hardware level, and its support for concurrency was added later.
  • When GO was born (2009), there were already computers with multi-core CPUs, so it made full use of multi-core CPUs in its design, so it supports high concurrency better.

1. Compiled language or interpreted language

Computer programming languages ​​can be divided into two types: compiled languages ​​and interpreted languages.

  • Compiled type: Compile the source code of the program with a compiler before running the program, and then run it directly with the compiled result.
  • Interpretation type: Every time you run a program, you need to get the source code and let the interpreter interpret and execute it.

insert image description here

Go is a compiled language that needs to be packaged into executable files under different operating system types to run on different platforms.

Java is neither a completely compiled language nor a completely interpreted language. It is a semi-compiled and semi - interpreted language .

  • Semi-compiled : It means that its code will be compiled into a class type file, and it only needs to be compiled once, and it can be executed on Java virtual machines of different operating systems;
  • Semi-interpretation : It means that in the Java virtual machine, it still needs to interpret the code of the class file sentence by sentence into the executable code of the corresponding operating system.

2. Microscopic comparison

1> Reference type parameter passing

  • Go can be passed by value (with an object as a parameter) or by reference (with an object pointer as a parameter).
  • Java can only be passed by reference

2> Object-oriented features

  • Go focuses on a simple, no aspect programming, generics, inheritance...
  • Java has many object-oriented features.

For more micro-level differences between the use of Go and Java languages, see the follow-up articles of the column.

3. Go application scenarios

A programming language is just a tool, there is no best one, only the most suitable one.

1. Server programming. Like Java, the most common application scenario of Go language is the development of server back-end systems, such as: microservice system development (Web application, API application), processing logs, data packaging, virtual machine processing, file system, etc.

2. Distributed system development. For example: cluster systems, game servers, database agents, middleware and other scenarios.

3. Network programming. Go is very suitable for network programming that requires high performance and high concurrency, and a system that uses Socket to transmit data to each other, similar to the use of Netty in Java.

4. Development of cloud native platform (operation and maintenance management system). At present, many foreign cloud platforms are developed using Go, such as: Docker, K8s, and many DevOps platforms.

5. Game system development.

6. Blockchain development. GO language is the first development language of blockchain.

The scenarios where the Go language is not suitable are:

  • Strong real-time software, such as: voice communication, unmanned driving, garbage collection, automatic memory allocation, etc.

1. Applications on open source

Representative technologies in cloud native technologies: docker, kubernetes, Prometheus, etcd, and consul are all developed using GO.

The author studies Go just to see the source code of these technologies.

4. Summary and follow-up

This article mainly introduces some common sense of Golang.

The column will introduce the use of Go language by analogy with Java, and use this to start the practice and source code reading of subsequent cloud-native open source technologies.

The next article continues the construction of the GO environment, the operation and operation principle of the Hello World code.

Guess you like

Origin blog.csdn.net/Saintmm/article/details/130793052