In the following code


(Cherolyn Lexvold) #1

https://play.golang.org/p/Ht1G6Ajz0wl

Why does case (4==4) not print?


(Hatnice) #2

Because when case 3==3 evaluates true the switch is done. Its done after that.


(J Ohn Stuart) #3

The first correct condition is 3==3. This gets executed then the program exists.


(Jay Ts) #4

This is from the Go Programming Language Specification:

In an expression switch, the switch expression is evaluated and the case expressions, which need not be constants, are evaluated left-to-right and top-to-bottom; the first one that equals the switch expression triggers execution of the statements of the associated case; the other cases are skipped. If no case matches and there is a “default” case, its statements are executed. There can be at most one default case and it may appear anywhere in the “switch” statement.

https://golang.org/ref/spec#Switch_statements

So that’s why only one of the fmt.Println() functions ran. It’s possible to get both to run by using a fallthrough statement, but unfortunately, the second will run whether the expression in its case is true or not. For example:

case (3 == 3):
		fmt.Println("prints")
		fallthrough
case (4 == 5):
		fmt.Println("also true, does it print?")   // yes, even though 4==5 is false!

https://play.golang.org/p/k1ON1NekeT7

Odd, isn’t it? In this type of switch, you may never want to use fallthrough. It’s more commonly used when the switch expression is non-empty, as in this example:

   package main

    import (
    	"fmt"
    )

    func main() {

    	var letter rune = 'f'
    	
    	switch letter {
    		case 'f':
    			fmt.Printf("The letter is Unicode %U\n",letter)
    			fallthrough
    		case 'g':
    			fmt.Printf("The letter is either an f or a g\n")
    		default:
    			fmt.Printf("The letter %c is not an f or a g\n",letter)
    	}
    }

https://play.golang.org/p/BhIOUPMO-kE

Try different values for letter, including 'f', 'g', and anything else (to trigger the default case). Notice that I used a rune type for letter, so you can use any Unicode character (like a :heartpulse: heart symbol or :joy_cat: kitty face - try cutting and pasting those; just remember to put single quotes around them) and the code will still work.


(Cherolyn Lexvold) #5

Thanks! That really helps ! I think I’m going to save this


(Cherolyn Lexvold) #6

Interesting


(Cherolyn Lexvold) #7

Thanks, gentlemen. All of your answers were helpful especially Jayts. Thank you all for your time.
Jayts. I tried f and g and enjoyed the results. I tried the heart pulse but got something like : invalid character… Not sure I put it in the right place. I put it here: var letter rune = :heartpulse:


(Jay Ts) #8

Hi Cherolyn,

I tried it myself and learned that copying and pasting the Unicode heart from my post doesn’t work. Maybe it needs to be quoted as code:

var letter rune = 💗

Try that. It worked for me just now.

package main
import "fmt"
func main() { fmt.Println("I💗Go") }

Working with Unicode can be a little tricky sometimes.


(Cherolyn Lexvold) #9

I did something wrong

https://play.golang.org/p/H2QfBDfdVP9


(Norbert Melzer) #10

Yes, single quotes were missing.

https://play.golang.org/p/ihUaSa0ze4J


(Cherolyn Lexvold) #11

Where should the single quotes go?


(Norbert Melzer) #12

Around the heart you assign to the variable, just as in my play-link


(Cherolyn Lexvold) #13

Cool. That worked. Generally, when do you use single quotes?


(Norbert Melzer) #14

For runes.


(Cherolyn Lexvold) #15

Thanks :slightly_smiling_face: