LoginSignup
0
0

go基本構文メモ

Last updated at Posted at 2023-09-14

すぐ見れるようにメモしておく

for

for i := 0; i < 10; i++ {
	sum += i
}

for (range)

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

for i, v := range pow {
	fmt.Printf("2**%d = %d\n", i, v)
}

if

if x < 0 {
	return sqrt(-x) + "i"
}

if short style

if v := math.Pow(x, n); v < lim {
    return v
}

if and else

if v := math.Pow(x, n); v < lim {
    return v
} else {
    fmt.Printf("%g >= %g\n", v, lim)
}

Switch

switch

switch os := runtime.GOOS; os {
case "darwin":
    fmt.Println("OS X.")
case "linux":
    fmt.Println("Linux.")
default:
    // freebsd, openbsd,
    // plan9, windows...
    fmt.Printf("%s.\n", os)
}

switch with no condition

t := time.Now()
switch {
case t.Hour() < 12:
    fmt.Println("Good morning!")
case t.Hour() < 17:
    fmt.Println("Good afternoon.")
default:
    fmt.Println("Good evening.")
}

Defer

defer

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")
}


文字列のフォーマット


text := fmt.Sprintf("id: %d, value: %s", index, value)
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