type switch 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")
}

4
1
Awgiedawgie 440215 points

                                    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{}
}

4 (1 Votes)
0
3.6
5
Phoenix Logan 186120 points

                                    package main

import (
	"fmt"
	"time"
)

func main() {

	isDay := time.Sunday

	switch isDay {
	case time.Monday:
		fmt.Println("day is monday")
	case time.Sunday:
		fmt.Println("day is sunday")
	case time.Tuesday:
		fmt.Println("day is tuesday")
	case time.Wednesday:
		fmt.Println("day is wenesday")
	case time.Thursday:
		fmt.Println("day is thursday")
	case time.Friday:
		fmt.Println("day is friday")
	case time.Saturday:
		fmt.Println("day is saturday")
	default:
		fmt.Println("day is not exist")
	}

	// multiple check
	switch browser := "firefox"; browser {
	case "chrome", "opera", "firefox":
		fmt.Println("my browser is", browser)
	default:
		fmt.Println("browser is not define")
	}

    // iflese style
	 var point = 6
      switch {
      case point == 8:
          fmt.Println("perfect")
      case (point < 8) && (point > 3):
          fmt.Println("awesome")
      default:
          {
              fmt.Println("not bad")
              fmt.Println("you need to learn more")
          }
      }
  
}

3.6 (5 Votes)
0
Are there any code examples left?
New code examples in category Go
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source