Brother even go language training Go programming language Assessment Report

Source: Internet
Author: User
Tags cloud computing platforms

Brother Lian chain Training Course System Design framework includes the basic language of the blockchain, go language, Blockchain backend technology system, blockchain public chain, blockchain distributed application development and other content, as well as to the final interview guidance and project combat. The course was carefully built by Tsinghua University's Microsoft Google Teacher team, which lasted half a year and developed together.

1. about Go

Go was developed by Google on September 21, 2007, open Source, November 10, 2009, 2012

The first official version of the Universal programming language was launched on March 28. It is 7 designed for system programming and is strongly typed

Language, has a garbage collection mechanism, and explicitly supports concurrent programming. The Go program is constructed from packages to provide efficient

Dependency management functionality. The current compiler implementation uses the traditional "compile-link" model to generate an executable binary

Thing

For more than 10 years, the mainstream system-level programming language has not appeared, but during this time, the computing environment has undergone a huge

Change. Here are some of the changing trends:

The speed of the computer becomes extremely fast, but software development is not fast enough.

Today, dependency management has become an important part of software development, but the traditional C-family language

The way to organize the source code, which runs counter to clear dependency analysis and fast compilation.

The type systems of languages such as Java and C + + are cumbersome, and people are resisting more and more, so they turn to

Dynamic type languages such as Python and JavaScript.

The popular system language is not good for the basic ideas like garbage collection and parallel computing.

The advent of multicore computers has caused some trouble and confusion.

As the system becomes larger and more demanding, the existing distributed system of language development becomes cumbersome and difficult to maintain.

And go is a new language, a concurrent, garbage-collected, fast-compiling language. It satisfies the following characteristics:

It can compile a large go program in a few seconds on a single computer.

Go provides a model for software construction that makes dependency analysis easier and avoids most C-style

The include file and the beginning of the library.

Go is a static type of language, and its type system has no hierarchy. Therefore, the user does not need to define the relationship between the types

It takes time to feel more lightweight than a typical object-oriented language.

Go is a completely garbage-collected language and provides basic support for concurrent execution and communication.

According to its design, go intends to provide a method for the construction of system software on multicore machines.

Go attempts to be a safe compiler for efficient and statically typed languages with easy, dynamic type language combined with interpretive programming

Statements were It is also intended to be a modern, support network with multi-core computing languages. To meet these goals, you need to address some of the language

Problem: A expressive but lightweight type system, concurrency and garbage collection mechanisms, strict dependency specifications, and so on.

These cannot be solved by libraries or tools, so go is the same.

2. The defects in C/

A. Initialization order of global variables

Because the order of initialization of global variables is not deterministic in C + +, it relies on global variable initialization.

Sequential operations can cause unpredictable problems for the program.

B. Variables are not initialized by default

Because variables are not initialized by default, it is possible to forget to initialize a variable in a program.

Some strange details have been made so that they are specifically emphasized in the coding standard.

C. Support for character sets 1

The character set that is first supported by C + + is ANSI. Although the support for Unicode is provided after the c99/c++98

, but in the daily coding work in the ANSI and Unicode to convert back and forth, rather cumbersome.

D. Complex inheritance patterns

C + + provides single/multiple inheritance, while multiple inheritance introduces a lot of complexity, such as "Diamond Inheritance" and so on.

For details, see the Deep Exploration C + + object model.

E. Support for concurrency

In C + +, concurrency is more common by creating threads, while communication between threads is

Enjoy variable to achieve, it is easy to deadlock. Although the concurrency processing mechanism has been added to the c++11, this

The miscellaneous type system adds a heavier burden.

F. Automatic garbage collection is not supported

There is a controversy over this point. Memory leaks are a common problem for C + + programmers, but as garbage

The maturity of the recovery algorithm, for most developers, the convenience of automatic recycling has been more than the manual operation improved

Efficiency. In C + +, although smart pointers can be used to reduce the use of native pointers, it cannot be eliminated, so this

The problem still exists.

G. Backward package management mechanisms

The use of. H/.C (PP) files to organize your code in C + + is a way to make compilation time too long

The template in C + + makes this problem even worse.

H. C + + compilers always generate some code privately

For example: constructor/destructor/new/delete and so on. If it is a dynamic library, include different versions of the header

Files, it is easy to generate incompatible versions of the code.

I. Unconstrained pointer use is one of the important causes of C + + software bugs

3. Benefits of Go

1

The encoding at compile time is also indeterminate. If the main.cpp is UTF-8 encoded, the behavior under GCC and VC Also

Will not be the same. --Wood Tree fir

As one of the language designers, Rob Pike said:

" We

the problems we are having when writing software. "

Most of these issues are in the 2nd section. This section is the way go addresses these flaws

Solutions.

A. Init

Each package can define one or more Init functions 2 (the prototype is Func init ()), and the Init function is initially

is called when imported, the order of execution of multiple init functions within the same package is indeterminate, and if the package is imported

Other packages, the Cascade calls, all packages import complete, and after all the init functions have been executed, the main

Yes.

For global variables, take a simple example to illustrate:

Package P

var gInt int

...

Package A

Import "P"

...

Package B

Import "P"

...

Package Main

Import (

A

"B"

)

...

In package p, we define a global variable gint, and P is import by the package A, B, and then

The package main is also sequentially import a A, a, that is, a before B is import. A imports the P first, so at this point gint is

Initialization, which solves the problem of inconsistent initialization order of global variables in C + +.

B. Default auto-initialization

Go introduces the concept of 0 values, that is, when each object is created, the default is initialized to its corresponding type of 0

Value. For example, the string is "", the pointer is nil,int to 0, and so on, so that the variable is used, not because forget

The initialization of the memory and some inexplicable problems arise. In addition, because of the introduction of the value of 0, the code is also convenient to write. Than

For example, the mutex type of the Sync pack, after introducing 0 values, can be used in the following ways:

2

Each source file can also contain multiple init functions. The order of calls between multiple init is indeterminate. The INIT function itself cannot be cited by other variables or functions

Use (Call or Fetch function address). --Wood Tree fir

3

var locker sync. Mutex

Locker. Lock ()

Defer Locker. Unlock ()

...

and the corresponding C + + code, you may want to write this:

Critical_section Locker

InitializeCriticalSection (&locker)

EnterCriticalSection (&locker)

...

LeaveCriticalSection (&locker)

DeleteCriticalSection (&locker)

Forgetting any one-step operation will result in a deadlock (dead lock) or other problem.

C. UTF-8

The go language natively supports UTF-8 encoding format 3. At the same time go involves the various packages of strings, also directly for

UTF-8 provides support, such as:

STR: = "Example"

if str = = "Example" {...}

D. Support only combination does not support inheritance

OOP is implemented in go by combining rather than inheriting, because "inheritance" has some drawbacks, such as

: "Does not adapt to change", "will inherit to the non-applicable function". Therefore, in coding practice, it is generally recommended that the combination be preferred

Non-inherited. In go the further, directly removed the inheritance, only supports the combination. When defining a struct, use anonymous

Implementation in C + +, and when defining interface, it is also possible to implement

Port inheritance. Like what:

Type A struct{}

Func (A A) Helloa () {

...

}

Type B struct{}

Func (b b) Hellob () {

...

}

Type C struct {

A

B

}

c: = &c{}

C.helloa ()

C.hellob ()

At this point C has the Helloa, Hellob two methods, that is, we can easily implement the "implementation of inheritance."

The UTF-8 with BOM is also supported. --Wood Tree fir

E. Go (Goroutine) and channels (channel)

Go's support for concurrency is based on the CSP model, which, when code is written, follows the "total

memory, rather than communicating through shared memory. To do this, go provides an abstraction called "Go". By

On Go is a higher-than-thread abstraction, so it is lighter and easier to use. And when more than one go requires

Communication, the channel becomes the bridge between them. For example:

Func Goroutine (pass chan bool) {

Fmt. Println ("Hello, I ' m in the Goroutine")

Pass <-True

}

Func Main () {

Pass: = Make (chan bool)

Go Goroutine (pass)

<-pass

Fmt. PRINTLN ("passed")

}

The code uses the keyword Chan to declare a channel, precede the function with the keyword go to open a new

Go away. This go will be automatically destroyed after the execution is completed. In the communication process, it is possible to pass the <-operator to the channel

Put or remove data from it.

F. Automatic garbage collection

Similar to languages like C #, Java, and so on, in order to save programmers from the mire of memory leaks, GO provides

The automatic garbage collection mechanism, and no longer distinguishes whether an object is from a stack or a heap (heap).

G. Interface (interface)

In addition to go, the biggest feature of the go language is the interface design, the interface of Go and Java interface,

C + + 's virtual base class is different, it is non-intrusive, that is, when we define a struct, we do not need to explicitly

Which one/several interface are implemented, and as long as a struct defines what a interface declares

All methods, it implicitly implements the interface, the so-called structural-typing (about duck-

For the difference between typing and structural-typing, refer to the relevant notes for minux.ma).

Suppose I want to define a interface called shape that has circle, Square, triangle, etc.

Class.

In languages like Java, we first abstract a interface from multiple implementations in the brain, namely:

When defining shape, we will first derive commonality from the implementation class. For example, they can all calculate polygons

Can be plotted, that is, shape has area and show methods. After defining the shape, set the

Semantic circle, Square, triangle, and so on, these classes are explicitly derived from shape, that is, we first implemented the

The interface implements the "implementation" again. In the implementation process, if the defined interface is found to be inappropriate because the implementation

To specify which base class it derives from, so at this point we need to refactor:

Public interface Shape {

Public float area ();

public void Show ();

}

public class Circle:implements Shape {

Public float area () {return ...}

public void Show () {...}

}

(similarly square and triangle)

In go, since interface is implicit and non-intrusive, we can first implement circle,

Square, triangle and other subclasses. In the implementation of these "implementation classes" process, due to the increase in knowledge, we can more

It is good to know which methods should be put into interface, that is, the refactoring is done in the process of abstraction.

Type Circle struct {}

Func (c Circle) area () float32 {}

Func (c Circle) Show () {}

(similarly square and triangle)

Type Shape Interface {

Area () float32

Show ()

}

So circle, square, and triangle achieve shape.

For a module, only the user of the module knows most clearly that it needs to be used by other

What methods are provided by the module, that is, the interface should be defined by the user. And when the user realizes it, they don't know

The way it will be used by the modules, so it just needs to realize their own good, do not need to care about the granularity of the interface is how thin

To be suitable for this kind of trivial problems. Interface is defined by the user on demand, without prior planning.

The advantages of Go interface compared to interface in Java are:

1. Define on demand, minimize the cost of refactoring.

2. First achieve the post-abstraction, collocation structure embedded, in the writing of large software, our module can be

The coupling degree of the organization is lower.

H. Go command

In order to compile the program under Unix/linux, you may need to write makefile or a variety of advanced self-

Build tool (Windows also has a similar tool, but is hidden behind a variety of powerful Ides).

One of the motivations for Rob Pike, who invented go, was: "Google's large C + + program compiles over time

Long. " So in order to achieve: "When compiling the Go program, as a programmer, apart from writing code, no need to write any matching

Files or something as extra. "This goal introduces the GO command family. With the Go command family, you can

Easy to get open source code from the implementation of online repository, compile and execute code, test code and other functions. This and C/

C + + is a big step forward compared to the way it is handled.

I. Automatic type deduction

Go is a compiled language, but while writing code, you can provide dynamic language

Flexibility. When defining a variable, you can omit the type and let the compiler automatically deduce the type for it, so

Reduced the number of input words for programmers. Like what:

I: = 0⇔var I int

s: = "Hello World" ⇔var s string = "Hello World"

J. Mandatory Coding style Specification

is K&r or ANSI the position of the braces in C + +, using tab or

Whitespace,whitespace is 2 characters or 4 characters and so on trivial issues and debated. Within each company

have defined their own coding standard to enforce the constraint. And as the internet boomed, the Open source project

More and more, these small problems affect everyone's work efficiency. And there is a programming guideline of "Less is more". For

Consistency, go provides a dedicated format command go FMT, which unifies everyone's coding style.

As a programmer, when you write code, you can write it in the style you like. When writing is complete,

By executing the Go FMT command, you can unify your code into the standard style of go. So you're in contact with a strange

The go code reduces the strangeness associated with coding style differences and emphasizes consistency.

K. Self-bringing unit testing and performance testing tools

Although the C + + does not provide the official unit testing and performance testing tools, there are a number of third-party related tools.

And because everyone in contact, like the tool may be different, resulting in the burden of communication. In view of this,

Go provides the official test tool go test, which allows you to easily write unit test cases. For example, this completes

The writing of a unit test:

Package test

Example.go

Func Add (A, b int) int {

Return a + b

}

...

Example_test.go

Func testadd (t *testing. T) {

Define a table to show the Table-driven test

Table: = []struct {

A, b, result int

}{

{1, 0, 1},

{1, 2, 3},

{-1,-2, 0},

}

For _, Row: = Range Table {

If Row.result! = Add (ROW.A, row.b) {

T.fatalf ("Failed")

}

}

}

Empathy performance testing.

You can complete the test by executing go test after writing.

L. Cloud Platform Support

In recent years, cloud computing has been booming, and go is known as the "C language of the 21st century", and of course it cannot be

Depending on the needs of this piece. There are a number of cloud computing platforms that support go language development, such as the officially maintained Gae,

Third-party AWS, and so on.

M. Simplified pointers

This may not be an advantage, the function of pointer arithmetic is very powerful, but the harm is very

Highlighted, so the pointer in go cancels the arithmetic function, only "reference/dereference" function is retained.

N. Simple syntax, quick start, easy to get started for new members

Go is essentially a C-family language, so if you have a C-family language experience, it's easy to get started.

4. The disadvantage of Go 4

A. The scheduler is not perfect

B. Native libraries too small/weak

C. 32bit-memory leaks

On this, go contributor minux.ma in the Golang-china discussion group explained in detail:

"The GC currently used by GO is a conservative GC, and in other words, it is better to waste less and not

Can mistakenly release the memory that is still in use, which is reflected in the design is to start from the stack, global variables, all possible

Is the pointer uintptr all as pointers, traversing, finding all the memory objects that can be accessed, and then putting the remaining

The release.

So how can you tell if a uintptr is a pointer? You know, the memory allocation for GO is a reference.

Tcmalloc, and made some changes. The original Tcmalloc is a tree-like structure using a page table that has been saved from the operation

The memory page obtained in the system, go uses another method. Because go needs to maintain some of each memory word

State (for example, does it contain pointers?) Is there a finalizer? Is the beginning of a struct? And the above mentioned is

can also access the state), combined with each word requires 4bit of information; so go first find an area

(arena), requesting access from the operating system with inaccessible permissions (mmap

The prot parameter is Prot_none), and then according to each uintptr corresponding 4 bits apply for a single RW of the internal

Storage (bitmap) corresponds to the previous arena, so that the address of the memory on the heap is known to obtain the corresponding bitmap address

It is very simple, do not need to look like Tcmalloc, direct simple right SHIFT and addition can be obtained;

4

Can be summed up as: performance/ecological/bug/tools and so on. --Wood Tree fir

The demand paging of the system will automatically handle bitmap that have not yet been used.

Everyone here understands why go uses such a large amount of virtual memory (arena) and knows why it often

When memory is low, panic says the requested memory is out of range (because memory is not in the range that bitmap can map

, of course, a number of bitmap can solve this problem, but it is not supported at present); Back to the beginning of the question

Question, since Arena has an address range, judging whether a uintptr may be a pointer is to determine whether in this range

In the.

This is the problem. If I had a int32, his content happened to be in that range, and more coincidentally

If you take it as a pointer and it happens to point to a large data structure, then the GC can only assume that the data structure is

In use. This creates a leak. This problem exists on 32-bit/64-bit platforms. But on the 32-bit

The problem is more serious, mainly 32-bit representation of the address space 768MB is arena, that is, an evenly distributed

The probability of UIntPtr is 768/4096, which is much larger than the 16gib/(2^64b) of the 64-bit system.

Go 1.1 Not surprisingly, it uses a record of the type of objects allocated on each heap to be almost complete

To solve this problem, it is almost complete because the data on the stack is not type, so there is

The problem that I said earlier, only will affect will be much smaller. ”

D. No strong IDE Support

E. Maximum available memory 16G limit

Because the go 1 was launched on March 28 this year, there is still a shortage of go. A, C, e these deficiencies in

In the early 2013 Go 1.1 will be resolved, and B, D will need to wait for the accumulation of time to perfect.

5. Go controversy

A. Error handling mechanism

In error handling, go does not provide an exception mechanism like C + +, Java, etc., but instead takes a check of the return value

The biggest controversy at the time of Go.

Reasons for opponents:

1. Every step, you have to do the examination cumbersome, primitive.

2. The type of error returned can be ignored by _.

3. The official error interface returned has only one error () method to output a string that cannot be used to determine

Complex error, than I define a method Openjsonfile (name string) (JFile jfile,

Err error), this method may lead to errors in two 1. File not found, 2, file parsing

Error, then I want to return an error with two messages, 1, error code, 2 wrong mention

Error codes, which are used in the program, error hints are used to quickly understand this error and are now

In the official error interface there is only a bug prompt, and there is no error code.

Reasons for support:

1. In the most recent place where the error occurred, you can handle the error in the best possible way.

2. The stack information thrown after the exception is crash, and the key information is disclosed for those who have ulterior motives;

For the end user, he will not see what is going on. The use of error mechanisms can

gives you the opportunity to replace stack information with more meaningful information to improve security and

User-friendliness.

3. Exceptions can also be handled by default.

B. New and variable initialization

In go, new and delete have different meanings in C + +. Delete is used to remove a

Map item, and new is used to obtain a pointer to a type object, because go supports syntax similar to the following

Type T struct {

...

}

Obj: = &t{}⇔obj = new (T)

At the same time go provides another keyword make to create the built-in object, so &t{} this syntax and

Make together, you can basically replace new (but the creation of a basic type of pointer such as new (int) is not available

&t{}, so new looks a bit redundant, which is somewhat inconsistent with the simple principle of go.

C. For...range cannot be used for custom types

For the convenience of traversal, go provides the for-range syntax, but this construct can only be used for built-in types,

such as slice, map, and Chan, and for non-built-in types, even if the relevant data structures in the official package container

No, this reduces the ease of use of for-range. Now, without support for generics, a very friendly

For-range still seems to be very difficult.

D. Dynamic linking is not supported

Currently go only supports static links (but Gccgo supports dynamic links, and go 1.1 may support some dynamic chains

This is another contentious area. Arguments on both sides of the argument are dynamic Link/static link of excellent, missing

Point, do not repeat here.

E. Non-generic

Most modern programming languages provide support for generics, while in go 1 there is no provision for generics

The support. Support generics either reduce compilation efficiency or reduce the program, according to Russ Cox, an official team member

efficiency, or reduce operational efficiency. And these three happen to be in conflict with Go's fast, efficient, easy-to-write goals

Of While go provides interface{} that reduces the expectations and requirements for generics, the need for generics can also be a competitive

The focus of the discussion.

F. First letter capitalization indicates visibility

Only package-level visibility is supported in go, regardless of the variable, structure, method, or function, if a large

The start of a letter, its visibility is public, it can be referenced in other packages, and if you start with a lowercase letter,

Its visibility is the package in which it is located. Because go supports UTF-8, and for words that are not case-sensitive like Chinese

The problem occurs when you need to export the characters. On this issue, the reason for supporters is: since the language itself supports

UTF-8, then the variable name should be consistent; The reason for not supporters is that Chinese names in Chinese, day

I named it in Japanese ... And if you want to write a symbol such as Chinese, you can add an English to the Chinese symbol.

Symbols. For example:

var cannot be exported int = 0

var e to export int = 0

6. Alternative options

A. Cgo

In front of the disadvantage section has said that go lacks native package, and now the world has a large number of C implementation of

High-quality third-party libraries such as OpenAL, OpenCL, OpenGL, etc. To solve this problem, go introduces

A command called CGO, by following a simple convention, you can wrapper a C library into a go package, which

The reason why go has a lot of high-quality packages in just a few years. CGO related examples are no longer shown here.

B. B/S

Because so far, go has not provided GUI-related support. Meanwhile, in the era of cloud computing, more and more

The program adopts B/s structure, and go provides the most perfect support for Web programming, so if the program needs to provide the interface

, whether it is a local program, or a server program, in the current proposal to use the B/S architecture instead.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.