0
0

More than 5 years have passed since last update.

「A Tour of Go」でGo言語を学ぶ(~#23)

Posted at

今回はif文です。
最近はコピペが続いてる...。

if.go
package main

import (
    "fmt"
    "math"
)

func pow(x, n, lim float64) float64 {
    if v := math.Pow(x, n); v < lim {
        return v
    } else {
        fmt.Printf("%g >= %g\n", v, lim)
    }
    // can't use v here, though
    return lim
}

func main() {
    fmt.Println(
        pow(3, 2, 10),
        pow(3, 3, 20),
    )
}

結果


27 >= 20
9 20

感想


if文も括弧記号がないこと以外は一緒。
見たことない機能として条件の前にちょっとした文が書ける機能があること。
vはifの中でしか使えないようになってるから、バグが減るようになってるのかな?

0
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
0
0