Go FAQs (i)

Source: Internet
Author: User
Tags creative commons attribution

Pure amateur interest in translation, ability limited level amateur, welcome to shoot bricks.
There are some sentences not translated, because I didn't understand:-)
The problem is more, the part translates, this document is the first section.
Original: Frequently asked Questions (FAQ)

Origin what is the purpose of the project?

The non-system underlying language has been around for more than 10 of years, and the computing environment has changed dramatically during that time. The following are some of the major trends:
-The speed of computer operation is greatly improved, but the efficiency of software development is not significantly improved.
-Dependency management is an important part of today's software development, but "header files" like the C language hinder simple dependency analysis and fast compilation.
-The rise of weak-type language programming, in contrast to languages like Java, C + +, and other types of heavy-type systems, has prompted more and more developers to program using dynamic type languages such as Python or JavaScript.
-Such basic concepts as garbage collection and parallel computing are not well supported in today's popular system languages.
-The advent of multi-core computers has raised more concerns and doubts.

We believe it is worth trying to develop a new language that supports concurrency, garbage collection, and can be quickly compiled. For the above points:

    • A large project that can quickly compile on a single machine (e.g. within seconds) go.
    • Go provides a software development model that makes dependency analysis easier and avoids the inconvenience of using dependencies like C on files and libraries.
    • The Go type system has no hierarchy, and all does not spend time on defining type relationships. Similarly, although go has a static type, it also tries to make the type appear lighter (as opposed to other classic object-oriented languages).
    • Go provides a complete garbage collection mechanism and provides basic support for concurrent execution and communication.
    • Based on its own design, go on the multi-core machine software system to build a solution.

For more detailed and valuable answers to this question, refer to the article Go at Google:language Design in the Service of software Engineering.

What is the status of the project?

Go became an open source project in 2009/11/10. After several years of positive development, in 2012/3/28 this day Go1.0 stable version officially released. This release contains language specifications, standard libraries, and a common library of tools that provide the foundation for creating reliable enterprise projects.

With a stable foundation, we use go to develop programs, projects, and tools rather than frequent language or library changes. In fact, the goal of GO1 is to provide a long-term stable development environment. Any changes on Go 1 are backwards compatible. We need to know what the future of go should look like, not just change the version number.

Of course, go will continue to improve, but the direction is focused on performance, reliability, portability, and the development of new features such as improved internationalization support.

There may be a go 2 version in the future, but for the current few years the main development go 1 (as of today, the latest Go1.6, officially released in 2016-01-27).

What ' s the origin of the mascot?

The mascot logo was designed by Renée French, who also designed Glenda, the Plan 9 bunny. The gopher was inspired by the WFMU t-shirt she wore a few years ago. This logo is used within the Creative Commons Attribution 3.0 License.

What's the history of the project?

Robert Griesemer, Rob Pike and Ken Thompson, began to paint a new language on the Whiteboard in September 2007. Soon the goal was hammered out, and the language had its original outline, and was gradually developed with the amateur effort of everyone. By the January 2008, Ken began to design a compiler that would output the C language to explore ideas. By the middle of the year, go has become a full-time project and is well prepared to develop its compilers. In May 2008, independently started on a GCC front end for Go using the draft specification (??). Russ Cox, who joined in the latter half of the year, began implementing the language rules of go and some base libraries through prototypes.

Go in 2009-11-10 member open source project. Many developers in the community contribute a lot of constructive advice and discussion, as well as code.

Why is you creating a new language?

Go was born as a driver of dissatisfaction with the existing language and system programming environment. Programming has become so difficult, and the choice of language has become confusing. Efficient compilation, efficient execution and easy programming these three are not available concurrently in the same mainstream language. The developer passively chooses a dynamic type language (such as python/javascript) for safe and efficient development, rather than C + + or java.

Go attempts to be both analytic type, dynamic type language as easy to develop, but also static, compiled language as efficient and secure. It also focuses on supporting networks and multicore programming.
Finally, you want to speed up compilation with go: It takes up to a few seconds on a single machine to complete the compilation process for a large project. To achieve these goals, a large number of such problems need to be addressed: a type system that is expressive and lightweight, concurrency and garbage collection, and so on. The existing libraries or tools do not solve these problems very well, so the new language is born.

Go at Google explains the background and motivations for the birth of the Go language, which allows you to find more answers to this question.

What is Go ' s ancestors?

Go is largely a C family (basic grammar), referencing and absorbing the virtues of pascal/modula/oberon (declarations, packages), combined with Tony Hoare's CSP theory (e.g. Newsqueak and limbo ) has made some improvements. But it's a whole new language. The go language design takes into account what developers can do and how to do it, at least as much as we do, and that means more fun.

What is the guiding principles in the design?

Now the programming contains too much bookkeeping, duplication and documentation work.
As Dick Gabriel says, reading the old program is like a quiet conversation between a good-talking researcher and a learning machine colleague, rather than a quarrel with the compiler. But who would have thought that sophistry would bring noise? "sophistication (complication?) There is a reason-no one wants to go back to the old language-but can it be more peaceful to achieve?

Go attempts to reduce the number of codes entered. Through its design, we try to reduce clutter and complexity. In go, there is no predecessor declaration and header file; All objects/variables are declared only once.
The initialization method is easy to read, automated and easy to use. The syntax is clean, the keyword is lightweight. You can use the structure of: = (declare and define) to simplify ordinary expressions: Foo. foo* Myfoo = new (foo. Foo). And most fundamentally, the go type has no hierarchy, and there is no need to declare the relationship between types. This simplified mechanism makes go easy to express and easy to understand.

Another important principle is to keep the concept orthogonal. Can implement any type of method;
Structs are used to represent data and interfaces are used to represent abstractions, and so on. Orthogonality makes it easier to understand what happens when you combine operations.

Using the is Google using Go internally?

Yes. There are already several go projects deployed in the production environment within Google. An external example is the backstage service of the golang.org website. It is a godoc-related service that runs in a production environment on Google APP engine.

Other examples are like vitess (for large-scale SQL installation), dl.google.com (Google download service, which provides chrome binaries and other large-volume installation packages).

Does Go programs link with a C + + programs?

Go has two types of compilers, GC and GCCGO. The GC uses a different calling convention (calling convention is the method by which the program passes parameters and gets the return value when the function is called: by register, or by a stack, or by a mixture of the two. ) and linker, so only C programs can be linked by the same calling convention, which is the C compiler instead of the C + + compiler. Gccgo, as the front end of GCC, is able to link to a gcc-compiled C + + program.

The CGO program provides an "external function interface" mechanism that makes it safe to call the C library in the Go code.
Swig expands the C + + library to support external calls.

Does Go Support Google ' s protocol buffers?

There is a separate open source project that provides the necessary compilation components and libraries. Address: https://github.com/golang/protobuf/

Can I Translate the Go home page into another language?

Of course. We encourage developers to build go language-related websites in their language.
However, if you choose to add Google Logos or brand to your site (not appearing on golang.org), you will need to follow the guidelines stated on www.google.com/permissions/guidelines.html.

Design what's up with Unicode identifiers?

It is important to extend the space of identifiers from the range of ASCII. The rule-indentifier character in go must be a letter or number defined in Unicode-easy to understand, easy to implement, but limited. The combination identifier is not currently within the design. Until there is an agreed external definition of what the identifier might be, plus a definition of canonicalization of Iden Tifiers that guarantees no ambiguity, it seemed better to keep combining characters out of the mix. Thus we have a simple rule that can is expanded later without breaking programs, one that avoids bugs that would surely AR Ise from a rule, that admits ambiguous identifiers.

In relation to this, because an exported identifier must start with uppercase, some identifier created by some or some of these can not be exported. The only solution at the moment is to use a similar X日本语 approach, but this is obviously a setback; we are considering other options. The rules for case-and-visibility-related (case-for-visibility) are unlikely to change because this is one of the most common features of go.

Why does Go don't have feature X?

Each language contains some new features and lacks some of the most favorite features of some people. Go's design focuses on coding happiness, compiling speed, orthogonal principles, and supporting features like concurrent execution and garbage collection. Your favorite feature may not be in go, probably because it doesn't fit, or it might affect the performance or cleanliness of the design, or it complicates the basic system model.

If go lacks certain features that affect your development, please forgive us and look at the features included in go. You may find some interesting remedy options.

Why does Go don't have generic types?

Generics may be added at some point. We don't think this is critical, although we know that some developers are desperate.

Generics provide convenience, but they increase the cost of type systems and run-time in complexity.
Even though we have been thinking about it, we haven't found a design that can correlate value with complexity. At the same time, the go built-in map and slice enhance the ability to use empty interface to build containers (by explicitly unpacking), which means that many times this encoding has reached the effect of using generics.

Keep an open question here and get more information from the generics proposal issue.

Why does Go don't have exceptions?

We think that the exception handling like Try-catch-finaly is coupled to the control structure,
will produce a confusing code. This also often encourages developers to identify too many general errors, such as failure to open a file.

Go takes a different approach. For general error handling, Go's multivalued return can report an error without returning results. A typical error type with go other features can make error handling very brisk and different from other languages.

Go also has a pair of built-in methods that support sending signals and recovering from exceptions when an exception occurs. The recovery mechanism executes only after an error in one part of the method and the state exception, which is sufficient to deal with a disaster without the need for additional control structures. Use good words to make the error handling code part more tidy.

Please refer to defer, Panic, and Recover for more detailed information.

Why does Go don't have assertions?

Go does not support assertions. Using assertions is undeniable convenience, but we think many people are using assertions to ignore how to properly handle and report errors. Proper error handling means that the program should continue to execute rather than crash after a non-fatal error occurs. The correct error report also means where the error occurred precisely, and the error stack information for the analysis was saved. Accurate errors are necessary, especially if the developer is not familiar with the current code.

We understand that this is the focus of the debate. In go, many things are different from modern practice theories simply because we feel it is necessary to try different implementations.

Why build concurrency on the ideas of CSP?

Concurrent and multithreaded programming is recognized as having difficulty. We consider this to be inseparable from complex designs (such as pthreads) and over-emphasis on the underlying details, such as mutexes, condition variables, memory barrier. Using the upper interface can make the code simpler, even if there are underlying concepts like locks.

One of the most successful models for concurrency to provide high-level language support comes from Hoare's communicating sequential Processes, or CSP model. The CSP model is also used in both the Occam and Erlang languages. The concurrent prototype of Go is derived from the other part of the model, the channel as the first class object. From the experience of the early several languages, it can be shown that the CSP model can be well integrated into the application language framework.

Why Goroutines instead of threads?

Goroutine is one of the reasons for implementing concurrent programming more easily. This idea has been around for some time, that is, multiplexing and executing multiple functions independently in a group of thread. When a coroutine is blocked, like calling a blocking system call, the go runtime automatically moves the other coroutine on the same thread on the same system thread to another thread, so there is no blocking. Programmers don't see this change, that's the point. As a result, calling coroutine can be very inexpensive: they have only a small overhead on the stack, usually just a few kilobytes of memory.

To make the stack smaller, the Go runtime uses a mutable, bounded stack. A new goroutine has a few kilobytes of space, which is basically enough to use. The go runtime automatically increases the space used for the storage stack, allowing the goroutine to have enough memory available. The average function call will incur the overhead of three micro instructions per CPU. In practice, you can create tens of thousands of goroutines in the same address space. If Goroutine is thread, then less quantity is consumed in light system resources.

Why is map operations not defined to be atomic?

After a long discussion, we decided that the typical usage of map does not require secure access in multiple Goroutine cases (these are the maps that are part of a large data set or calculation that has been synchronized). Requiring all map operations to be locking slows down the program and does not significantly improve security. This is not an easy decision, because a runaway map access can ruin the entire program.

The language does not preclude atomic map updates (go does not exclude the atomic update of map??).
When required, such as when hosting a untrusted program, the implementation could interlock map access (if an atom update is required, such as executing an Trusted program, this implementation (atomic operation) can lock the access of map??).

Would you accept my language change?

Developers often suggest improvements to the language-the mailing list contains a history of such content-but few suggestions are accepted.

Although go is an open source project, it is also protected by compatibility, which avoids breaking existing project programs because of changes. If your proposal violates the Go 1.x specification, we can't accept the idea, even if it's excellent. The future major release version may not be compatible with Go 1.x, but we're not going to discuss what it looks like in the future.

Even if your proposal complies with the Go 1.x specification, it does not necessarily conform to the design purpose of go. For reference here, go at Google:language Design in the Service of software engineering explains the origins of go and the motives behind its design.

Type is Go object-oriented language?

Go can say yes, or it can be not oo language. Although go has types and methods for OO programming, there is no type hierarchy. The concept of interface in Go provides a radically different way of being easy to use and more versatile in some ways. You can also nest a type to other types to achieve similar sub-class effects. Also, the go approach is more common than in C + + or Java: You can define methods for any type of data, even built-in types like integers, not just structs or classes.

Also, because of the lack of a type hierarchy, object is more portable in go than other languages such as C++/java.

How do I get dynamic dispatch of methods?

The only way to dynamically dispatch a method is through an interface. Methods on structs or other concrete types are always parsed statically.

Why is there no type inheritance?

OO programming, at least in some mainstream languages, involves too many of these discussions about type relationships and relationships that can be automatically derived. Go takes a different approach.

Unlike the relationship between the two types that require a programmer's predecessor declaration, an object in Go will implement the interface automatically whenever it implements all the methods of an interface. This approach is more advantageous by reducing the burden of explicitly declaring relationships. Object types can implement multiple interfaces at the same time without having to bear the complexities of traditional multiple inheritance. Interface can be very lightweight-the interface can have only one or even no method. Interfaces can added after the fact if a new idea comes along or for Testing-without annotating the original types. Because in the There is no explicit relationship binding between the image and the interface, and there is no need to manage the type hierarchy.

With these idea, you can build something like a type-safe UNIX pipeline. Consider, for example, FMT. fprintf How to format the output of any data, not just a file; Another example is how the Bufio package is completely independent from the file I/O; or image package how to generate a compressed picture file. All these ideas from a interface (IO. Writer, in fact, a method writer) derived from. And that's just a superficial effect. The interface of Go has a profound impact on the entire program structure.

This will take some time to adapt, and this type of implicit dependency style is a feature of Go's most built-in power.

Why is the ' Len ' a function and not a method?

We also discussed this issue, and ultimately decided to implement Len as a function rather than a method, which would not complicate the interface of the underlying type (in the Go sense).

Why does Go overloading of methods and operators?

If you do not need to do type matching, function scheduling will be simple. Experience with other languages tells us that it is occasionally useful to use multiple duplicate names, but in practice it can be confusing. Matching by name only, and requiring the same type, is a major simplification in the go system.

For operator overloading, it seems convenient, but it is not necessary. Without them, things would be easier.

Why doesn ' t Go has "implements" declarations?

A type in go to implement an interface, as long as the implementation of all the methods of the interface can be, without him. This mechanism allows the interface to be freely defined and used without having to modify existing code. This type of structuring facilitates separation of concerns while improving code reuse and building a code development model. The semantics of the interface is one of the main reasons why go claims to be flexible and lightweight.

For more information, please refer to type inheritance

How can I guarantee my type satisfies an interface?

You can ask the compiler to verify that the type T implements interface I, such as assigning a value of 0 to a type T or a type pointer t:

typestruct{}var _ I = T{}       // Verify that T implements I.var _ I = (*T)(nil// Verify that *T implements I.

If T or T does not implement interface I, it will be error-free at compile time.

If you want the user of interface to explicitly declare that an interface is implemented, you can add a method whose name has this meaning to the method set of the interface. Like what:

typeinterface {    Foo()    ImplementsFooer()}

The type must then implement the Fooer method Implementsfooer, which is noted here in Godoc.

typestruct{}func (b Bar) ImplementsFooer() {}func (b Bar) Foo() {}

Most of the code does not use this restriction because it is a bit confusing (limit the utility of the interface idea). However, sometimes this can solve the problem caused by a similar interface causing confusion.

Why doesn ' t type T satisfy the Equal interface?

As shown below, objects can be compared by interface methods (the same type is not necessarily the same value):

typeinterface {    bool}

and type T:

typeintfuncboolreturn// does not satisfy Equaler

Unlike most cases in polymorphic systems, T does not implement interface Equaler. The t.equal parameter type is T, not the required type Equaler.

In go, the type system does not automatically derive the parameters of the equal; This responsibility is the developer who can define the type T2 to implement the interface Equaler:

typeintfuncboolreturn t == u.(T2) }  // satisfies Equaler

Although this is not the same as other types of systems, any type that implements Equaler in go can be passed as a parameter to T2.equal, and at run time it is necessary to check whether the parameter is of type T2. Some languages ensure that this type is consistent during compilation.

Another relevant example:

typeinterface {   Open() Reader}func (t T3) Open() *os.File

In go, this type of T3 does not implement interface opener.

In this case, the go type system does not do much for programmers, and the lack of a sub-type mechanism makes it simple to implement the rules of the interface: is the name and parameter signature of the function exactly the same as defined in interface? The rules of Go are also easy to implement effectively. We think these advantages can compensate for the pitfalls of Go in automatic type deduction. Whether or not one day go should take the form of polymorphic classes, we expect a way to express the ideas of these examples and be able to perform static checks.

Go FAQs (i)

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.