This is a creation in Article, where the information may have evolved or changed.
The impulse to learn two days go, although there are a lot of bright spots, but feel nothing to attract me (not as simple as Ruby, unified grammar, there is no Lisp as powerful abstract mechanism).
- Simple and straightforward concurrency mechanism
Go provides a concurrency mechanism called goroutine. "It is called goroutine because the existing phrases-threads, processes, process, and so forth-pass inaccurate meanings." Goroutine has a simple model: it is a function that executes in parallel with other goroutine and has the same address space. It is lightweight and consumes only a little more than the allocation stack space. The initial stack is very small, so they are also inexpensive, and are allocated (and freed) on the heap space as needed. "[Learning Go Language" • 7th Chapter]
Goroutine's syntax is simple, add the keyword go before a function:
Ready ("Tee", 2) //normal function call go ready ("Tee", 2)//Ready () Run as Goroutine
The following example comes from the Go language introductory tutorial--eratosthenes prime Sieve method.
Before we begin, we have to introduce the mechanism--channel for goroutine communication in the go language. The channel is like a two-way pipeline under UNIX, and its syntax is simple:
CI: = make (chan int)
This creates a channel,ci that can send and receive integers.
Here's the point--the Eratosthenes sieve method that goes with go:
As shown, pour the number of filters into a filter FN (which will filter out multiples of all n), and repeat the process as input to the next filter.
Generate () is the equivalent of initialization, which is crammed into the channel from 2 onwards. In a round of screening, the first number that comes out of the channel is definitely prime, with which it screens out the composite for this round of screening, and then plugs the remaining number into another channel (as input for the next round of screening).
Package Mainimport FMT "FMT"//Send the sequence 2, 3, 4, ... to channel ' ch '. Func Generate (Ch Chan int.) {for I: = 2; ; i++ { ch <-i //Send ' I ' to Channel ' ch '. }} Copy the values from channel ' on ' to channel ' out ',//removing those divisible by ' prime '. Func filter (in, Out Chan int, Prime int) {for { I: = <-in //Receive value of the new variable ' I ' from ' in '. If I% prime! = 0 {out <-i //Send ' I '-channel ' out '. } }} Func Main () { ch: = make (chan int) //Create a new channel. Go Generate (CH) //Start Generate () as a goroutine. For { Prime: = <-ch fmt. PRINTLN (Prime) ch1: = make (chan int) go filter (CH, ch1, prime) ch = ch1 }}
- No previous self-increment + +
You don't have to dwell on i++ and ++i anymore, go only provides after self-increment, namely i++.
The syntax of GO is a big difference from C in that the declaration order is reversed: The variable name is declared first, and then the type is qualified. In fact, this is not important, go is more important than the improvement of C is that it can be type deduction, that is, according to the type of the assigned value to infer the type of the variable. Like what:
s: = "Hello, world"
It is assumed that the variable s is of type string, depending on the value assigned to the string. One notable place is ": =", which is typically used to define variables by passing the initialization (only in the body of the function).
The declaration of a function is also more special, the return type is declared in the final (the impression that the return type of C is written in the first bar):
func foo (input string) (output int) { return 4
}< pre>
Even more specifically, there is an option in front of the function name to specify the recipient of the function (the following example shows that the function bla () must be called by a variable of type S):
< Span class= "S1" >
package MainType s struct {i int}func (P S) bla () int {return P.i}func main () {V: = s{4}println (V.bla ())}
is estimated to be useful in object-oriented programming.
(continued)
P.s. Finally, the mascot of Go-lang and Plan 9, see figure