開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第2章(プログラム構造)、2.6(パッケージとファイル)、練習問題2.1の解答を求めてみる。
コード
tempconv.go
package tempconv
import (
"fmt"
)
type Celsius float64
type Kelvin float64
const (
AbsoluteZeroC Celsius = -273.15
FreezingC Celsius = 0
BoilingC Celsius = 100
)
func (c Celsius) String() string { return fmt.Sprintf("%g℃", c) }
func (k Kelvin) String() string { return fmt.Sprintf("%gK", k) }
func CToK(c Celsius) Kelvin { return Kelvin(c + 273.15) }
func KToC(k Kelvin) Celsius { return Celsius(k - 273.15) }
sample1.go
package main
import (
"fmt"
"プログラミング言語Go/tempconv"
)
func main() {
cs := []tempconv.Celsius{
tempconv.AbsoluteZeroC,
tempconv.FreezingC,
tempconv.BoilingC,
}
ks := []tempconv.Kelvin{}
for _, c := range cs {
k := tempconv.CToK(c)
fmt.Println(k)
ks = append(ks, k)
}
for _, k := range ks {
fmt.Println(tempconv.KToC(k))
}
}
入出力結果(cmd(コマンドプロンプト)、Terminal)
$ go run sample1.go 0K 273.15K 373.15K -273.15℃ 0℃ 100℃ $
0 コメント:
コメントを投稿