mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-24 23:29:01 +00:00
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 180.
|
|
|
|
// Package tempconv performs Celsius and Fahrenheit temperature computations.
|
|
package tempconv
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
)
|
|
|
|
type Celsius float64
|
|
type Fahrenheit float64
|
|
|
|
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9.0/5.0 + 32.0) }
|
|
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32.0) * 5.0 / 9.0) }
|
|
|
|
func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
|
|
|
|
/*
|
|
//!+flagvalue
|
|
package flag
|
|
|
|
// Value is the interface to the value stored in a flag.
|
|
type Value interface {
|
|
String() string
|
|
Set(string) error
|
|
}
|
|
//!-flagvalue
|
|
*/
|
|
|
|
//!+celsiusFlag
|
|
// *celsiusFlag satisfies the flag.Value interface.
|
|
type celsiusFlag struct{ Celsius }
|
|
|
|
func (f *celsiusFlag) Set(s string) error {
|
|
var unit string
|
|
var value float64
|
|
fmt.Sscanf(s, "%f%s", &value, &unit) // no error check needed
|
|
switch unit {
|
|
case "C", "°C":
|
|
f.Celsius = Celsius(value)
|
|
return nil
|
|
case "F", "°F":
|
|
f.Celsius = FToC(Fahrenheit(value))
|
|
return nil
|
|
}
|
|
return fmt.Errorf("invalid temperature %q", s)
|
|
}
|
|
|
|
//!-celsiusFlag
|
|
|
|
//!+CelsiusFlag
|
|
|
|
// CelsiusFlag defines a Celsius flag with the specified name,
|
|
// default value, and usage, and returns the address of the flag variable.
|
|
// The flag argument must have a quantity and a unit, e.g., "100C".
|
|
func CelsiusFlag(name string, value Celsius, usage string) *Celsius {
|
|
f := celsiusFlag{value}
|
|
flag.CommandLine.Var(&f, name, usage)
|
|
return &f.Celsius
|
|
}
|
|
|
|
//!-CelsiusFlag
|