Golang Notes-Concurrent

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

  the main function in the Go language is also run in a separate goroutine, commonly called main At the end of the Goroutine,main function, the execution of the other goroutine is interrupted, but the other goroutine does not interrupt the execution of the other goroutine unless it is terminated by the communication itself.

Let's look at one of the simplest concurrency examples, a time server: (tricky, should not use this example, should be highlighted, this example can be placed in the TCP section)

Func Main () {Listener, err:= Net. Listen ("TCP","localhost:8000")    ifErr! =Nil{log. Fatal (ERR)} for{conn, err:=Listener. Accept ()ifErr! =Nil{log. Print (ERR) Continue} go Handleconn (conn)}}func Handleconn (c net. Conn) {defer c.close () for{_, Err:= Io. WriteString (c, time. Now (). Format ("2006/01/02 15:04:05\n"))        ifErr! =Nil {            return} time. Sleep (1*Time . Second)}}

As can be seen from this example, using go to open a TCP service is very concise, and from this example, we can see the way to format the date and time in Go is very wonderful, the format template is limited to Mon Jan 2 03:04:05pm 2006 UTC-0700, You can remember this: January 2 3:4 P.M. 5 Seconds (200) 6 years UTC-0700.

You can either use NC or Telnet to connect to the TCP service for testing, or you can use go to implement a simple client:

Func Main () {    conn, err:= net. Dial ("tcp""localhost:8000")    if Nil {        log. Fatal (Err)    }    defer conn. Close ()    mustcopy (OS. STDOUT, conn)}func mustcopy (DST io. Writer, src io. Reader) {    ifnil  {        log. Fatal (Err)    }}

Goroutine is communicated through the channel, including sending and receiving two kinds of operations. It is possible to declare a channel such as CH = make (chan int), like a map, and the channel is simply a reference to the underlying data structure, so both the sender and the receiver will reference the same object, and the channel is a reference type, so its 0 value is nil. The operator used by the channel is <-, which refers to sending data to the channel, and receiving refers to receiving it from the channel. Channel is similar to the General IO system, you can close a channel by close (CH), after shutting down, will not be able to send operations, but can receive the previously not received data, if there is no data to receive, then received a nil.

The channel created by make (Chan int) is called a no-cache channel, indicating that the channel capacity is 0, and if you want to declare a cached channel, you can use make (chan int, 100). The second parameter represents the channel capacity size at initialization time.

A send operation based on no cache channels will cause the sender to block Goroutine until another Goroutine performs a receive operation on the same channels, and when the sent value is successfully transmitted through channels, Two goroutine can continue execution of subsequent statements. Conversely, if a receive operation occurs first, the receiver Goroutine will also block until another Goroutine performs the send operation on the same channels.

Simply put, when using the no cache channel, once the communication is initiated, it must wait until the communication is complete before it can continue, otherwise it will block the wait. This means that no cache channel will force a synchronous operation when communicating, so no cache channel is also called Synchronous channel

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.