Go Learning 1

Source: Internet
Author: User

1 Package Main2 3 Import (4     "Bufio"5     "FMT"6     "Math"7     "OS"8     "Runtime"9 )Ten  One Constresult ="Polar Radius=%.02fθ=%.02f°→cartesian x=%.02f y=%.02f\n" A  - varprompt ="Enter a radius and an angle (in degrees), e.g., 12.5 ,"+ -         "or%s to quit." the  -Type Polarstruct { - radius float64 - Θfloat64 + } -  +Type Cartesianstruct { A x float64 at y float64 - } -  - func init () { -     ifRuntime. GOOS = ="Windows" { -prompt = Fmt. Sprintf (Prompt,"Ctrl + Z, Enter") in}Else{//Unix-like -prompt = Fmt. Sprintf (Prompt,"Ctrl+d") to     } + } -  the Func Main () { *Questions: =Make (chan polar) $ defer close (questions)Panax NotoginsengAnswers: =Createsolver (questions) - defer Close (answers) the interact (questions, answers) + } A  the func Createsolver (Questions Chan Polar) Chan Cartesian { +Answers: =Make (Chan Cartesian) - go func () { $          for { $Polarcoord: = <-Questions -θ:= polarcoord.θ* Math. Pi/180.0 //degrees to radians -x: = Polarcoord.radius *Math. Cos (θ) theY: = Polarcoord.radius *Math. Sin (θ) -Answers <-cartesian{x, y}Wuyi         } the     }() -     returnAnswers Wu } -  About Func Interact (Questions Chan Polar, answers Chan Cartesian) { $Reader: =Bufio. Newreader (OS. Stdin) - FMT. Println (Prompt) -      for { -Fmt. Printf ("Radius and angle:") ALine, err: = Reader. ReadString ('\ n') +         ifErr! =Nil { the              Break -         } $         varRadius,θfloat64 the         if_, Err: = FMT. SSCANF (Line,"%f%f", &radius, &θ); Err! =Nil { theFmt. Fprintln (OS. Stderr,"Invalid input") the             Continue the         } -Questions <-polar{radius,θ} inCoord: = <-Answers the FMT. Printf (result, radius,θ, coord.x, Coord.y) the     } About FMT. Println () the}

Go Learning 1

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.