Switch better than if-else in Go?

func If(i int) bool {
if i == 1 {
return true
} else {
return false
}
}
func Switch(i int) bool {
switch {
case i == 1:
return true
default:
return false
}
}
func SwitchConst(i int) bool {
switch i {
case 1:
return true
default:
return false
}
}
BenchmarkIf
BenchmarkIf-8 1000000000 0.268 ns/op 0 B/op 0 allocs/op
BenchmarkSwitch
BenchmarkSwitch-8 1000000000 0.267 ns/op 0 B/op 0 allocs/op
BenchmarkSwitchConst
BenchmarkSwitchConst-8 1000000000 0.263 ns/op 0 B/op 0 allocs/op
BenchmarkIf
BenchmarkIf-8 1000000000 0.270 ns/op 0 B/op 0 allocs/op
BenchmarkSwitch
BenchmarkSwitch-8 1000000000 0.265 ns/op 0 B/op 0 allocs/op
BenchmarkSwitchConst
BenchmarkSwitchConst-8 1000000000 0.276 ns/op 0 B/op 0 allocs/op

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store