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