"Go if else"의 두 판 사이의 차이

 
5번째 줄: 5번째 줄:
// https://gobyexample.com/if-else
// https://gobyexample.com/if-else
package main
package main
import "fmt"
import "fmt"
func main() {
func main() {
    if 7%2 == 0 {
if 7%2 == 0 {
        fmt.Println("7 is even")
fmt.Println("7 is even")
    } else {
} else {
        fmt.Println("7 is odd")
fmt.Println("7 is odd")
    }
}


    if 8%4 == 0 {
if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
fmt.Println("8 is divisible by 4")
    }
}


    if num := 9; num < 0 {
if num := 9; num < 0 {
        fmt.Println(num, "is negative")
fmt.Println(num, "is negative")
    } else if num < 10 {
} else if num < 10 {
        fmt.Println(num, "has 1 digit")
fmt.Println(num, "has 1 digit")
    } else {
} else {
        fmt.Println(num, "has multiple digits")
fmt.Println(num, "has multiple digits")
    }
}
}
}
</syntaxhighlight>
</syntaxhighlight>

2022년 3월 10일 (목) 11:43 기준 최신판

1 개요[ | ]

Go if else
// https://gobyexample.com/if-else
package main

import "fmt"

func main() {
	if 7%2 == 0 {
		fmt.Println("7 is even")
	} else {
		fmt.Println("7 is odd")
	}

	if 8%4 == 0 {
		fmt.Println("8 is divisible by 4")
	}

	if num := 9; num < 0 {
		fmt.Println(num, "is negative")
	} else if num < 10 {
		fmt.Println(num, "has 1 digit")
	} else {
		fmt.Println(num, "has multiple digits")
	}
}
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),
	)
}

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}