“Type Switch Golang” Réponses codées

Type Switch Golang

var x interface{} = "foo"

switch v := x.(type) {
case nil:
    fmt.Println("x is nil")            // here v has type interface{}
case int: 
    fmt.Println("x is", v)             // here v has type int
case bool, string:
    fmt.Println("x is bool or string") // here v has type interface{}
default:
    fmt.Println("type unknown")        // here v has type interface{}
}
Difficult Dotterel

interrupteur Golang

func main() {
	i := 2
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }
}
Radu Cuziac

interrupteur Golang

	package main 
    
    my_number := 2
    fmt.Print("Write ", my_number, " as ")
    
    switch my_number {
      case 1:
          fmt.Println("one")
      case 2:
          fmt.Println("two")
      case 3:
          fmt.Println("three")
      default :
          // default case 
    }

Long Lemur

Switch Type Golang

var value interface{} = "Hello Grepper"
switch vale.(type){
	case string:
    	fmt.Println("It is a string")
    case int:
    	fmt.Println("It is a int")
    default:
    	fmt.Println("Not sure")
}
Zany Zebra

Communiquez dans Golang

switch day {
  case "sunday":
    // cases don't "fall through" by default!
    fallthrough

  case "saturday":
    rest()

  default:
    work()
}
Harendra

Réponses similaires à “Type Switch Golang”

Questions similaires à “Type Switch Golang”

Plus de réponses similaires à “Type Switch Golang” dans Go

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code