Go 기초
Go Operators and Arithmetic
Why is 17 / 5 equal to 3 in Go? Learn arithmetic, comparison and logical operators.
배울 내용
- + - * / and %
- Integer division
- &&, || and ++
Expression → result
17 / 5317 % 52float64(17) / float64(5)3.4math.Pow(2, 10)1024How Go evaluates an expression
Operators combine values into expressions. Go follows the usual math rules, with one thing to remember: dividing two integers drops the fraction.
17 / 5 is 3 because both numbers are integers, and 17 % 5 is the remainder 2. Converting both sides to float64 keeps the fraction. && is true only when both comparisons are true. count++ adds one and is a statement, not an expression.
핵심 포인트
- int / int drops the fraction: 17 / 5 is 3.
- % gives the remainder: 17
%5 is 2. - Convert to float64 to keep the fraction.
실행 가능한 Go 예제
Go
main.go
package main
import (
"fmt"
"math"
)
func main() {
a, b := 17, 5
fmt.Println(a+b, a-b, a*b)
fmt.Println(a/b, a%b)
fmt.Println(float64(a) / float64(b))
fmt.Println(a > b && b != 0)
fmt.Println(math.Pow(2, 10), math.Sqrt(144))
count := 0
count++
count += 10
fmt.Println(count)
}
22 12 85
3 2
3.4
true
1024 12
11연습
예제를 온라인 컴파일러에서 열고 값을 바꾸거나 한 줄을 추가한 다음, 실행하기 전에 출력을 예상해 보세요. 그런 다음 일부러 코드를 망가뜨리고 컴파일러 메시지를 읽어 보세요.
이 레슨을 마쳤나요?
학습 완료로 표시하면 진행 상황을 기록할 수 있습니다.
진행 상황이 이 브라우저에 저장되어 있습니다 로그인하면 모든 기기에서 이어갈 수 있습니다.
챌린지로 연습하기
이 레슨에서 배운 내용으로 실제 문제를 풀어 보세요. 모든 챌린지는 브라우저에서 실행되며 즉시 테스트와 AI 리뷰를 받을 수 있습니다.
- Rectangle Perimeter Compute the perimeter of a rectangle from its sides. 쉬움
- 단리 계산 단리 이자를 적용한 후의 총 금액을 구하세요. 쉬움