Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page
  • Output
  • Explanation:

Was this helpful?

Edit on GitHub

If Else

This Go code demonstrates the use of conditional statements (if and else) and the use of the short declaration (:=). Let's go through each part of the code:

package main

import "fmt"

func main() {
    // Check if 7 is even or odd
    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }

    // Check if 8 is divisible by 4
    if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }

    // Check if either 8 or 7 is even
    if 8%2 == 0 || 7%2 == 0 {
        fmt.Println("either 8 or 7 are even")
    }

    // Using the short declaration to declare a variable 'num' and check its value
    if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
}

Output

7 is odd
8 is divisible by 4
either 8 or 7 are even
9 has 1 digit

Explanation:

  1. if 7%2 == 0 {...} else {...}: Checks if the remainder of dividing 7 by 2 is equal to 0. If true, it prints "7 is even," otherwise, it prints "7 is odd."

  2. if 8%4 == 0 {...}: Checks if 8 is divisible by 4 (i.e., the remainder is 0). If true, it prints "8 is divisible by 4."

  3. if 8%2 == 0 || 7%2 == 0 {...}: Checks if either 8 or 7 is even (i.e., the remainder is 0 when divided by 2). If true, it prints "either 8 or 7 are even."

  4. if num := 9; num < 0 {...} else if num < 10 {...} else {...}: Uses the short declaration to declare a variable num and initialize it with the value 9. It then checks various conditions:

    • If num is less than 0, it prints "9 is negative."

    • If num is less than 10 (but not negative), it prints "9 has 1 digit."

    • If neither condition is true, it prints "9 has multiple digits."

These conditional statements help control the flow of the program based on the given conditions. The short declaration is often used in Go for concise variable declaration and initialization within a block.

PreviousFor LoopNextSwitch

Last updated 1 year ago

Was this helpful?