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)是怎么調(diào)用的以下方法呢:func(cCelsius)String()string{returnfmt.Sprintf("%g°C",c)}func(fFahrenheit)String()string{returnfmt.Sprintf("%g°F",f)}這兩個(gè)方法都是有函數(shù)名String的。不太明白這個(gè)調(diào)用過(guò)程。
Go語(yǔ)言圣經(jīng)中函數(shù)調(diào)用的疑問(wèn)
慕容3067478
2019-05-24 15:43:44