Golang If-Else Branching

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The if-else challenge in Golang is designed to test your knowledge of branching with if and else statements in Go.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/if_else("`If Else`") subgraph Lab Skills go/if_else -.-> lab-15403{{"`Golang If-Else Branching`"}} end

if-else

You are required to complete the checkNumber function that takes an integer as input and returns a string. If the number is even, return "even", otherwise return "odd".

Requirements

  • The function should be named checkNumber.
  • The function should take an integer as input.
  • The function should return a string.
  • If the number is even, return "even".
  • If the number is odd, return "odd".

Example

$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit

## There is no [ternary if](https://en.wikipedia.org/wiki/%3F:)
## in Go, so you'll need to use a full `if` statement even
## for basic conditions.

Summary

The if-else challenge in Golang tests your knowledge of branching with if and else statements. In this challenge, you were required to complete the checkNumber function that takes an integer as input and returns a string. If the number is even, return "even", otherwise return "odd".

Other Go Tutorials you may like