Answers for "go if"

Go
4

go if else

func main() {
	// Basic one
	if x > 10 {
		return x
	} else if x == 10 {
		return 10
	} else {
		return -x
	}

	// You can put one statement before the condition
	if a := b + c; a < 42 {
		return a
	} else {
		return a - 42
	}

	// Type assertion inside if
	var val interface{}
	val = "foo"
	if str, ok := val.(string); ok {
		fmt.Println(str)
	}
}
Posted by: Guest on January-12-2021
1

if statements in go

a := 4
b := 6
if a + b == 10 {
  fmt.Println("a + b is equal to 10!")
} else {
  fmt.Println("a + b does not equal 10...")
}
Posted by: Guest on October-07-2020
1

go if

func sqrt(x float64) string {
	if x < 0 {
		return sqrt(-x) + "i"
	}
	return fmt.Sprint(math.Sqrt(x))
}

func main() {
	fmt.Println(sqrt(2), sqrt(-4))
}
Posted by: Guest on January-12-2021

Browse Popular Code Answers by Language