处理摄氏与华氏温度转换

4年以前  |  阅读数:302 次  |  编程语言:Golang 

tempconv处理摄氏与华氏温度转换。

tempconv.go

package tempconv

import "fmt"

type Celsius float64
type Fahrenheit float64

const (
    AbsoluteZeroC Celsius = -273.15
    FreezingC     Celsius = 0
    BoilingC      Celsius = 100
)

func (c Celsius) String() string    { return fmt.Sprintf("%g°C", c) }
func (f Fahrenheit) String() string { return fmt.Sprintf("%g°F", f) }

conv.go

package tempconv

// CToF converts a Celsius temperature to Fahrenheit.
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) }

// FToC converts a Fahrenheit temperature to Celsius.
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }

Go语言圣经第二章示例代码

 相关文章:
处理摄氏与华氏温度转换
Golang官方的HelloWorld
使用包tempconv进行摄氏与华氏温度的转换
并发的使用http.get获取内容,并打印出时间与内容
使用flag包打印命令行
使用strings包方法,计算文件名
HTTP服务器,返回请求参数及HTTP头部
打印所有的命令行参数
统计并打印输入的内容
读取通过命令行传入的文件,打印多次出现的行
最小HTTP服务器
使用strings.Join打印所有的命令行参数
格式化输出,打印水的温度
字符串运算示例之输出文件名
从文件或标准输入中读取内容,统计并打印出现多次的行
HTTP服务器,通过URL接口输出请求次数