"Go Language Programming" study (v)

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

One, concurrent

Package Mainimport ("Bufio"    "FMT"    "Math"    "OS")Constresult ="Polar Radius=%.02fθ=%.02f°→cartesian x=%.02f y=%.02f\n"varprompt ="Enter a radius and an angle (in degrees), e.g., 12.5 ,"+"or%s to quit."type Polarstruct{radius float64 tt float64}type Cartesianstruct{x float64 y float64}func init () {Prompt= Fmt. Sprintf (Prompt,"Ctrl+d")}func Main () {questions:=Make (Chan polar) defer close (questions) answers:=Createsolver (questions) defer close (answers) Interact (questions, answers)}func Createsolver (Questions Chan Pola R) Chan Cartesian {answers:=Make (Chan Cartesian) go func () { for{polarcoord:= <-Questions TT:= polarcoord.tt * Math. Pi/ thex:= Polarcoord.radius *Math. Cos (TT) y:= Polarcoord.radius *Math. Sin (TT) answers<-cartesian{x, Y}}} ()    returnAnswers}func Interact (Questions Chan Polar, answers Chan Cartesian) {reader:=Bufio. Newreader (OS. Stdin) fmt. Println (Prompt) for{fmt. Printf ("radius and angle:") line, err:= Reader. ReadString ('\ n')        ifErr! =Nil { Break        }        varradius, TT float64if_, Err: = FMT. SSCANF (Line,"%f%f", &radius, &AMP;TT); Err! =Nil {fmt. Println (OS. Stderr,"Invalid input")            Continue} questions<-Polar{radius, tt} coord:= <-answers FMT. Printf (result, RADIUS, TT, coord.x, COORD.Y)} fmt. Println ()}

The main function does a few things

1. Create a questions Channel

2. Create a Solver channel to start the calculation process

3. Carry out the main loop (with no conditional for)

This program uses the go process, the creation method of the process: go function. The process is communicating with the master, so two channels are created, one channel is the main process writes the data, the threads are read, one channel is created, and the main process reads.

Two, the co-process.

Goroutine is a live method invocation of a function that executes concurrently with other goroutine completely independent of each other. Each go program has at least one goroutine, which executes the main goroutine of the main function in the main package. Goroutine are very much like lightweight threads or co-processes, and they can be created in batches. All goroutine have the same address space, while the go language provides a lock primitive to ensure that the data is securely shared across Goroutine. However, the concurrent programming method recommended by the go language is communication, not data sharing.

With respect to the channel, if the capacity of a buffer is given, then the communication is asynchronous, as long as the buffer has unused space for sending data, or also contains the data that can be received, then its communication reception is non-blocking. If there is no buffer channel, or the channel is full, then it will become synchronous communication.

Goroutine when receiving data, you can use Select to listen to multiple channels simultaneously, any one channel has data, it will activate the corresponding channel processing.

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.