2019年3月5日火曜日

開発環境

プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第3章(基本データ型)、3.6(定数)、3.6.1(定数生成器 iota)、練習問題3.13の解答を求めてみる。

コード

package main

import "fmt"

func main() {
 const (
  KB = 1 << (10 * (iota + 1))
  MB
  GB
  TB
  PB
  EB
  ZB
  YB
 )
 // int型にしたら、ZBとYBについて、
 // # command-line-arguments
 // ./sample13.go:22:13: constant 1180591620717411303424 overflows int
 // ./sample13.go:23:13: constant 1208925819614629174706176 overflows int
 // というエラーに
 // とりあえずfloat64型に
 units := []float64{KB, MB, GB, TB, PB, EB, ZB, YB}
 for _, unit := range units {
  fmt.Println(unit)
 }
}

入出力結果(cmd(コマンドプロンプト)、Terminal)

C:\Users\...>go run sample13.go 
1024
1.048576e+06
1.073741824e+09
1.099511627776e+12
1.125899906842624e+15
1.152921504606847e+18
1.1805916207174113e+21
1.2089258196146292e+24

C:\Users\...>

0 コメント:

コメントを投稿