mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 22:52:05 +00:00
good good study, day day up!
This commit is contained in:
66
vendor/gopl.io/ch7/tempconv/tempconv.go
generated
vendored
Normal file
66
vendor/gopl.io/ch7/tempconv/tempconv.go
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
// 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
|
64
vendor/gopl.io/ch7/tempconv/tempconv.go.~master~
generated
vendored
Normal file
64
vendor/gopl.io/ch7/tempconv/tempconv.go.~master~
generated
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
|
||||
// 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
|
Reference in New Issue
Block a user