Where is the ternary conditional operator?

yourbasic.org/golang

Go has no ternary conditional operator. Instead of

res = expr ? x : y

you write

if expr {
	res = x
} else {
	res = y
}

In some cases, you may want to create a dedicated function.

func Min(x, y int) int {
	if x <= y {
		return x
	}
	return y
}

Share this page: