Assign this or that in Go

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Go

In Go, "this or that" is typically represented using conditional statements, such as "if-else" or "switch-case". These statements allow you to choose between multiple options based on certain conditions.

Here's an example of using an "if-else" statement to assign a value to a variable based on a condition:

package main
  
import "fmt"
  
func main() {
    x := 10
    var y string
  
    if x > 5 {
        y = "this"
    } else {
        y = "that"
    }
  
    fmt.Println(y)
}

In this example, we declare a variable x and assign it the value 10 and another empty variable y. Then, we use an "if-else" statement to check if x is greater than 5. If it is, we assign the value "this" to y, otherwise we assign the value "that". The final output will be "this" since x is greater than 5.

Here's an example of using a "switch-case" statement to assign a value to a variable based on a condition:

package main
  
import "fmt"
  
func main() {
    x := 2
    var y string
  
    switch x {
    case 1:
        y = "this"
    case 2:
        y = "that"
    default:
        y = "neither this nor that"
    }
  
    fmt.Println(y)
}

In this example, we declare a variable x and assign it the value 2 and another empty variable y. Then, we use a "switch-case" statement to check the value of x. If x is 1, we assign the value "this" to y. If x is 2, we assign the value "that" to y. If x is anything else, we assign the value "neither this nor that" to y. The final output will be "that" since x is 2.