Go Switch

Updated:2019-11-28
Share:

switch statement tests a variable value for equality against a defined list of values. every value is wrapped in a case clause, variable is checked on every case till a match found, which executes the code in that case body and stops there )

Switch Forms

Switch has two forms or types, but closely related, these forms/types as follows:

  • Expression Switch : where each case contains expressions, which is compared against the value of the switch expression.   
    switch expr { 
      case expr1 :
        .. 
      case expr2 :
        ..
      default : // optional 
         ..
    } 
    
    The following rules apply to a switch:
    • If no expression provided then the boolean value is true assumed.
    • You may have any number of case statements within switch. Each case is followed by the value to be compared to then a colon.
    • Constants in a case must be the same data type as the variable in the switch.
    • break is optional .
    • default case is optional too, but if used it must appear at the end of the switch.
    • default case is used if no case matched.

      Here is an Example:

      package main
      
      import "fmt"
      
      func main() {
      	host := "CoderMe"
      	switch host {
      	case "CoderMe":
      		fmt.Println(host)
      	case "google":
      		fmt.Println("!host(1)")
      	case "altavista":
      		fmt.Println("!host(2)")
      	default:
      		fmt.Println("404")
      	}
      }
      

      When you run  the previuos code, you'll get: 

      CoderMe
      
    • Type Switch syntax for a type switch statement in Go  is as follows :

      switch x.(type){
      case type:
        ..
      case type:
        ..
      /* you can have any number of case statements */
      default: // optional
       ..
      }
      

      Rules of using Type Switch:

      • The expression used in a switch statement must have an variable of interface{}.

      • You can have any number of case statements within switch.

      • Each case is followed by the value to be compared to and a colon.

      • Type of case must be match data type variable in the switch, and it must be a valid data type.

      • break is is optional and implied.

      • default case is optional, which must appear at the end of the switch. Check out the following example:

        package main
        
        import "fmt"
        
        func main() {
           var a interface{}

        a = false
        switch i := a.(type) { case int: fmt.Printf("a is int") case float64: fmt.Printf("a is float64") case func(int) float64: fmt.Printf("a is func(int)") case bool, string: fmt.Printf("a is bool or string") default: fmt.Printf("a type is: %T", a) } }

        When the above code is compiled and executed, it produces the following result −

        a is bool or string
        

Read also

Debian Router - Setup

In this articles series you will learn how to setup Linux (Debian) as a Router for home/business local networks, to spice things I'll show how to add dns server, proxy/web caching server, and basic web traffic filter to our cute debian box.   In this series I'll go over: Introduction . Hardware Requirements . Software installation . Basic Setup . /etc/network/Interfaces Configuration . Unbound dns server setup . DHCP server setup . Squid setup. Final step i...

Postgresql Stream Replication Setup

Recently Postgresql Stream replication become even easier, In this article you'll be shown how to setup stream replication for postgresql 9.3+.   Postgresql Stream Replication stream replication is a feature that let a mirror/replica/standby database to get a stream of changes that have been done on another primary database, to be replicated on as many standby databases as needed.   Use Cases stream replication can be used as follows: As Part of Data protection plan, Wh...