Answers for "go switch"

Go
4

go switch statement

switch time {
	case "morning":
		fmt.Println("Time to wake up!")
	case "night":
  		fmt.Println("Time to go to bed.")
	default:
  		fmt.Println("Enjoy life")
}
Posted by: Guest on September-22-2020
1

go switch

package main

import (
	"fmt"
	"runtime"
)

func main() {
	fmt.Print("Go runs on ")
	switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("OS X.")
	case "linux":
		fmt.Println("Linux.")
	default:
		// freebsd, openbsd,
		// plan9, windows...
		fmt.Printf("%s.\n", os)
	}
}
Posted by: Guest on January-12-2021
2

go switch case

switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("It's the weekend")
    default:
        fmt.Println("It's a weekday")
    }
Posted by: Guest on August-25-2020
0

golang switch

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")
          }
      }
  
}
Posted by: Guest on March-08-2021
0

go switch

// switch statement
    switch operatingSystem {
    case "darwin":
        fmt.Println("Mac OS Hipster")
        // cases break automatically, no fallthrough by default
    case "linux":
        fmt.Println("Linux Geek")
    default:
        // Windows, BSD, ...
        fmt.Println("Other")
    }

    // as with for and if, you can have an assignment statement before the switch value
    switch os := runtime.GOOS; os {
    case "darwin": ...
    }

    // you can also make comparisons in switch cases
    number := 42
    switch {
        case number < 42:
            fmt.Println("Smaller")
        case number == 42:
            fmt.Println("Equal")
        case number > 42:
            fmt.Println("Greater")
    }

    // cases can be presented in comma-separated lists
    var char byte = '?'
    switch char {
        case ' ', '?', '&', '=', '#', '+', '%':
            fmt.Println("Should escape")
    }
Posted by: Guest on January-12-2021

Browse Popular Code Answers by Language