LoginSignup
1
0

More than 3 years have passed since last update.

Goクイズ: int = float64

Last updated at Posted at 2020-12-22

以下のコードを実行すると何が起こるでしょうか?

package main

import "fmt"

const a int = 2

type number = int
type int = float64

func main() {
    const b int = 2
    const c number = 2
    fmt.Println(1 / a, 1 / b, 1 / c)
}
  1. 0 0.5 0 が出力される
  2. 0.5 0.5 0 が出力される
  3. 0.5 0.5 0.5 が出力される
  4. コンパイルエラー

回答
0.5 0.5 0.5 が出力されます. block と scope の問題で type alias はあまり関係ありません.
Go標準の int 等の識別子は最も外側のブロックである universe block に存在し,内側のブロックで再宣言可能です.また,上記コードで宣言した numberintpackage block なので main パッケージ全体で有効です.言い換えれば,上記のコードに現れる inttype 宣言の前か後かに関わらず全て main パッケージの int でそれは float64 を指します.なのでa,b,cは全て浮動小数点数型になります.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0