cf/main.go//Copyright©2016AlanA.A.Donovan&BrianW.Kernighan.//License:https://creativecommons.org/l...//Seepage43.//!+//CfconvertsitsnumericargumenttoCelsiusandFahrenheit.packagemainimport("fmt""os""strconv""gopl.io/ch2/tempconv")funcmain(){for_,arg:=rangeos.Args[1:]{t,err:=strconv.ParseFloat(arg,64)iferr!=nil{fmt.Fprintf(os.Stderr,"cf:%v\n",err)os.Exit(1)}f:=tempconv.Fahrenheit(t)c:=tempconv.Celsius(t)fmt.Printf("%s=%s,%s=%s\n",f,tempconv.FToC(f),c,tempconv.CToF(c))}}//!-tempconv/tempconv.go//Copyright©2016AlanA.A.Donovan&BrianW.Kernighan.//License:https://creativecommons.org/l...//!+//PackagetempconvperformsCelsiusandFahrenheitconversions.packagetempconvimport"fmt"typeCelsiusfloat64typeFahrenheitfloat64const(AbsoluteZeroCCelsius=-273.15FreezingCCelsius=0BoilingCCelsius=100)func(cCelsius)String()string{returnfmt.Sprintf("%g°C",c)}func(fFahrenheit)String()string{returnfmt.Sprintf("%g°F",f)}//!-tempconv/conv.go//Copyright©2016AlanA.A.Donovan&BrianW.Kernighan.//License:https://creativecommons.org/l...//Seepage41.//!+packagetempconv//CToFconvertsaCelsiustemperaturetoFahrenheit.funcCToF(cCelsius)Fahrenheit{returnFahrenheit(c*9/5+32)}//FToCconvertsaFahrenheittemperaturetoCelsius.funcFToC(fFahrenheit)Celsius{returnCelsius((f-32)*5/9)}//!-上面程序中的f:=tempconv.Fahrenheit(t)c:=tempconv.Celsius(t)是怎么调用的以下方法呢:func(cCelsius)String()string{returnfmt.Sprintf("%g°C",c)}func(fFahrenheit)String()string{returnfmt.Sprintf("%g°F",f)}这两个方法都是有函数名String的。不太明白这个调用过程。
添加回答
举报
0/150
提交
取消