go brief language, and enhance understanding

1. Go language has no concept of classes and inheritance, and so it  Java  or C ++ does not look the same. But it is achieved through the concept of polymorphism of the interface (interface) is. Go have a clear and understandable language lightweight type system, between the level of type he did not say. Therefore we can say the Go language is a hybrid language.

2. all things are interface

3. Go language designers all say, because the design of the Go language is C ++ gives them a sense of failure. In the Google I / O 2012's Go Design Team to meet at, Rob Pike had this to say:

We did a lot of C ++ development, tired of waiting for completion of the translation, although this is a joke, but to a large extent it is a fact.

 go language is a compiled language, but go without waiting for a long time to compile. This is his great advantage.

4. Go is a compiled language

Go compiler to compile the code. The compiler compiles the source code into a binary (or byte code) format; when the code is compiled, the compiler to check for errors, and optimize the performance of the binary output can be run on different platforms. To create and run the Go program, the programmer must perform the following steps.

    1. Go to create a program using a text editor;
    2. save document;
    3. Compiler;
    4. The resulting run the compiled executable file.

This differs from Python, Ruby and JavaScript language, they do not contain compilation step. Go comes with a compiler, so no need to install the compiler alone.

The main objective 5. Go language is the language of the safety and efficiency of static and dynamic language of ease of development organically combine to achieve the perfect balance, so that the program becomes more fun, instead of hard choices in the front line of pain .

Dynamic languages: (Dynamic programming Language - dynamic language or dynamic programming language), dynamic languages ​​are programs that can change its structure at runtime, new functions can be introduced, existing functions can be deleted and other changes in the structure.

Statically typed languages: (Statically Typed Language- statically typed language) statically typed language with dynamically typed languages ​​just the opposite, its data type is checked at compile the meantime, that when writing a program to declare the data types of all variables, C / C ++ is a typical representative of statically typed languages, as well as other statically typed languages ​​C #, JAVA and so on. For the distinction between dynamic languages ​​and static languages, to paraphrase a popular saying is: Static typing when possible, dynamic typing when needed. For example: the definition of a variable time, var ..... is a dynamic language, behind may modify the type of a variable int ..... it clear that this is a digital type, which is a static language.

6. Why Go language learning
If you want to create a system program, or Web-based applications, Go language is a very good choice. As a relatively new language, it is by experienced and respected computer scientists design, designed to meet the challenges faced by the program to create a network of large-scale concurrent.

Go before language appeared, developers are always faced with very difficult choices, what is the use of fast execution speed, but speed is not ideal compiled languages (eg: C ++), or use a compiler is faster but poor efficiency in the implementation of the language (eg: .NET, Java), or less difficult to develop but the execution speed of dynamic languages in general it? Obviously, Go language between these three conditions to achieve the best balance: fast compilation, efficient execution, ease of development.

Go language support cross-compiler, for example, you can run  Linux  to develop applications that can run on a Windows computer system. This is the first door fully supported programming languages UTF-8, which is not only reflected in the string it can handle using UTF-8 encoding, even its source file formats are used UTF-8 encoding. Go language did truly international!
 

7. go complicated by language

Go concurrent language is based on the goroutine, goroutine similar thread, but not threads. goroutine it can be understood as a kind of virtual threads. Go language runtime scheduling goroutine be involved, and goroutine reasonably allocated to each CPU, maximizing the use of CPU performance.

 

8. go concurrency, goroutine

Go concurrent language is based on the goroutine, goroutine similar thread, but not threads. Goroutine it can be understood as a kind of virtual threads. Go language runtime scheduling goroutine be involved, and goroutine reasonably allocated to each CPU, maximizing the use of CPU performance.

In plurality goroutine, Go language channels (channel) for communication channel is a built-in data structures , allows users to send messages with synchronization between the different types of goroutine. This makes the programming model is more inclined to send messages between goroutine, rather than more goroutine for rights to the same data.

Programs can be designed to require concurrent link producers and consumers of mode mode, the data into the channel. Another source end of the channel to the data and returns the result calculated concurrently, as shown in FIG.

Tip: Go language can achieve memory sharing between multiple goroutine through the channel.

[Example] Manufacturer generating a second string to pass through the channel and consumers, producers goroutine run concurrently using two consumers () function for processing goroutine the main.

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. )
  7. // data producers
  8. func producer(header string, channel chan<- string) {
  9. // infinite loop, non-stop production data
  10. for {
  11. // random number and the format string is a string sent to the channel
  12. channel <- fmt.Sprintf("%s: %v", header, rand.Int31())
  13. // wait one second
  14. time.Sleep(time.Second)
  15. }
  16. }
  17. // consumer data
  18. func customer(channel <-chan string) {
  19. // non-stop access to data
  20. for {
  21. // extract data from the channel, where the channel will block until the data is returned
  22. message := <-channel
  23. // print data
  24. fmt.Println(message)
  25. }
  26. }
  27. func main() {
  28. // create a string type of channel
  29. channel := make(chan string)
  30. // create producer () function concurrent goroutine
  31. go producer("cat", channel)
  32. go producer("dog", channel)
  33. // Data Consumption Function
  34. customer(channel)
  35. }

operation result:

dog: 2019727887
cat: 1298498081
dog: 939984059
cat: 1427131847
cat: 911902081
dog: 1474941318
dog: 140954425
cat: 336122540
cat: 208240456
dog: 646203300

Analysis of the code:

  • Line 03, introduced format (FMT), a random number (math / rand), time (time) packet from build.
  • Line 10, the function of production data, passed in a string tag type and a channel can only be written.
  • Line 13, for {} constitute an infinite loop.
  • Line 15, using rand.Int31 () generates a random number, using fmt.Sprintf () function formatted as a header and a random number string.
  • Line 18, using time.sleep () function and then perform one second pause function. If executed goroutine, the pause will not affect the execution of other goroutine.
  • Line 23, the function of consumption data, can only pass a written passage.
  • Line 26, construct a cycle of constantly consuming messages.
  • Line 28, taken from the data channel.
  • Line 31, the extracted print data.
  • Line 35, a program entry point, is always performed at the start of the program.
  • Line 37, examples of a character string type of channel.
  • Line 39 and line 40, concurrent execution of a producer function, two lines were created two goroutine this function with different parameters.
  • Line 42, to perform the function for data consumption through consumer channels.


Whole sections of the code, there is no thread creation, no thread pool and no lock, only to realize goroutine, and channel data exchange by keyword go. 

 

 

References:  http://c.biancheng.net/view/1.html

Guess you like

Origin www.cnblogs.com/ITPower/p/11921207.html