create an if statement with an else if and else with a condition of type string in go

In Go, you can create an if statement with an else if and else block using the following syntax:

main.go
if condition1 {
  // code to run if condition1 is true
} else if condition2 {
  // code to run if condition1 is false and condition2 is true
} else {
  // code to run if both condition1 and condition2 are false
}
213 chars
8 lines

To create an if statement with a condition of type string, you can simply use the == operator to check for equality between the string and the desired value. Here is an example:

main.go
package main

import "fmt"

func main() {
    name := "Alice"
    
    if name == "Bob" {
        fmt.Println("Hello, Bob!")
    } else if name == "Alice" {
        fmt.Println("Hello, Alice!")
    } else {
        fmt.Println("I don't know who you are.")
    }
}
264 chars
16 lines

In this example, if the value of the name variable is "Bob", "Hello, Bob!" will be printed. If the value of the name variable is "Alice", "Hello, Alice!" will be printed. Otherwise, "I don't know who you are." will be printed.

gistlibby LogSnag