This is a creation in Article, where the information may have evolved or changed.
Golang Study notes: frequently asked questions
tags (space delimited): Golang
Refer to golangfrequently asked Questions, a good reference document, understanding Golang must-read.
1. Golang characteristics and purpose of launch
The characteristics of Golang and the problems to be solved are summed up as three points:
1. Concurrent: Multi-core solution, language level concurrency, goroutine
2. Deficiencies of garbage-collected language:c/c++
3. Fast compilation:c/c++, such as the lack of reliance on simple, type system simple, non-traditional oo. Easier and faster to develop.
This 简单设计 feature is easy to compare with C + +, compared to C + + is cut 90% features, reduce the hassle of 90%. A better contrast might be c,better C with Goroutine and garbage-collection.
2. Golang Design Principles
- Felicity of programming: as much as possible to simplify the code writing rules, which in various interpretation of language, c++11, etc. can be reflected in a part of the embodiment of Golang is such as the definition of the package, compiled installation, no header files, no forward declarations,:= type inference and so on
- Orthogonality of concepts: Another principle is that conceptual design is as orthogonal as possible orthogonal, which makes it easier to understand. Methods can implemented for any type; Structures represent data while interfaces represent abstraction; And so on. Orthogonality makes it easier to understand what happens when things combine. Of course, once the design is orthogonal, the concept of need becomes less.
- Speed of compilation
3. Why there is no generic type
Generics is added at some point. We don ' t feel an urgency for them. Why generics are required to refer to this article, but the benefits of generics can also increase complexity, Golang may later add generic support, which is currently an open issue.
4. Why is there no exceptions
This is an important reason for Golang to be criticized by users (especially C++,java users), in fact Golang provides panic,recover syntax similar to a try catch. But the individual understands why it's not just a matter of choice, not a technical one. In many language coding styles, especially objective-c, it is common to use Error object to pass errors, although the performance loss of Try catch is negligible now, but the disadvantage of try catch is easy to misuse, Causes the user to ignore the difference between the error and the exception, in addition Golang provides the multiple return value also facilitates the error to pass this style the use, I personally to this kind of design does not resent.
5. Why is there no assertion
And no exceptions a bit similar: programmers use them as a crutch to avoid thinking about proper error handling and reporting of course this farfetched statement made The person himself is a little bit out of the understand: We are a point of contention. There is many things in the Go language and libraries this differ from modern practices, simply because we feel it ' s some Times worth trying a different approach.
6. Why build concurrency on the ideas of CSP
(Communicating sequential Processes)
One is the experience of Erlang,occam and so on, the second is to facilitate the construction of language level.
7. Why Goroutines instead of threads
The complexity of threading control is transferred from user space to the language level (the user level does not require a relationship or thread, so that concurrency is simple and efficient for its design purposes). The Go Scheduler
8. Why map is not thread safe
Knowing that map is not thread safe, this design is mostly a trade off
9. Is golang in the face of object language?
This problem is not right, the face of the object is more a design, rather than language features, but the implementation of different languages and support is different. In Golang the way is interface, no type hierarchy, subclass way a bit like C, more general than C + + and so on. At the same time Golang inside the method also more general, can give any type to add methods, overall is lightweight, more simple but can do more things. Personally think is the most beautiful design in the Golang, the interviewer asked you will not face the object programming, you hit his face.
10. Why Golang does not have an operator or method overload
There's nothing to say, operators and method overloads are useless. Do not use even in languages that provide such functionality (unless some extreme occasions such as the data processing framework overloaded +* notation are used for the OP matrix, this is a very rare occasion, and more often this feature brings about 1% convenience and 1000% less code clutter and readability).
One of interface's questionable examples why doesn ' t type T satisfy the Equal interface
typeinterface { bool}typeintfuncboolreturn// does not satisfy Equaler
Because Equaler's equal function requires different types, the correct implementation is
typeintfuncboolreturn t == u.(T2) } // satisfies Equaler另一个例子typeinterface { Open() Reader}func (t T3) Open() *os.File//T3 does not satisfy Opener, although it might in another language.
The Golang does not have an automatic type conversion and is not polymorphic (between classes), which is also the author's trade off
An example of error and nil
error { nil if bad() { p = ErrBad } returnreturn a non-nilerror.}
This is related to the implementation of interface:
Under the covers, interfaces is implemented as, a type and a value. The value, called the interface ' s dynamic value, is a arbitrary concrete value and the type is that of the value. for the INT value 3, an interface value contains, schematically, (int, 3).
An interface value was nil if the inner value and type are both unset, (nil, nil). In particular, a nil interface would always hold a nil type. If We store a nil pointer of type *int inside an interface value, the inner type would be *int regardless of the value of T He pointer: (*int, nil). Such an interface value would therefore be non-nil even when the pointer inside is nil.
一个更直观点的例子typestruct{}funcstringreturn"" }varnilvar error2 error = error1// to interface => error2 != nil
Go does not support tagged or untagged union
Untagged Union Unsafe, tagged union or variant types, algebraic type and interface have coincident
14. Why there is no implicit type conversion
The author thinks that this kind of function brings more hidden trouble than convenience, Golang Int and int64 are not a type and cannot be converted implicitly.
. Why is maps, slices, and channels references while arrays is values?
Just know that. The slices implementation is a reference to the underlying array (see), reference
make([]int, 10)fmt.Println("sa:", saa)//saa: [0 0 0 0 0 0 0 0 0 0]sb := saa[1:8]sb[2] = 2append(sbb, 8//sa 也被修改了fmt.Println("sa:""sb:", sb)// saa: [0 0 0 2 0 0 0 0 8 0] sbb: [0 0 2 0 0 0 0 8]
When should I use a pointer to an interface
Almost never. Passing a pointer to interface is a mistake in most of the times. Another: The insight is so although a pointer to a concrete type can satisfy an interface, with one exception a pointer to an I Nterface can never satisfy an interface.
17. An example of closures
funcMain () {done: = Make(Chan BOOL) Values: = []string{"a","B","C"} for_, V: =RangeValues {Go func() {FMT. Println (v) Done <-true}() }//wait for all goroutines to complete before exiting for_ =RangeValues {<-done}}//output is C c C because V uses a variable and the output depends on the FMT. The value of v stored when println is called //The correct wording is for_, V: =RangeValues {Go func(Ustring) {FMT. PRINTLN (u) done <-true} (v)}//or make a local copy for_, V: =RangeValues {V: = V//Create a new ' V '. Note the position is in Func or not. Go func() {FMT. Println (v) Done <-true}() }
18. No? : operator
Poor reviews