Go-Grundlagen

Go Operators and Arithmetic

Why is 17 / 5 equal to 3 in Go? Learn arithmetic, comparison and logical operators.

  • Einsteiger
  • 3 Min. Lesezeit
  • 1 ausführbares Beispiel
Das lernst du
  • + - * / and %
  • Integer division
  • &&, || and ++
Expression → result
17 / 53
17 % 52
float64(17) / float64(5)3.4
math.Pow(2, 10)1024

How 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.

Das Wichtigste

  1. int / int drops the fraction: 17 / 5 is 3.
  2. % gives the remainder: 17 % 5 is 2.
  3. Convert to float64 to keep the fraction.

Ausführbares Go-Beispiel

Go
Code ausführen →
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)
}
Run-Button für dieses Beispiel erstellen Ergebnis der Ausführung
22 12 85
3 2
3.4
true
1024 12
11

Üben

Öffne das Beispiel im Online-Compiler, ändere einen Wert oder füge eine Zeile hinzu und sage die Ausgabe voraus, bevor du es ausführst. Bring den Code danach absichtlich zum Scheitern und lies die Meldung des Compilers.

Lektion fertig?

Markiere sie als gelernt, um deinen Fortschritt zu verfolgen.

Weiter: Reading User Input →

Mit einer Challenge üben

Nutze den Inhalt dieser Lektion, um ein echtes Problem zu lösen. Jede Challenge läuft im Browser, mit sofortigen Tests und einem KI-Review.

Alle Challenges