Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"switch true {...}" or "switch {...}"? really? So how would

  switch 3.141592654 { ... }
affect the case-statements inside?


When you say "switch <value>", it matches the "case" statements based on if the value after "case" is equal to <value>. So, if you say

    const P = 3.141
    switch 3.141 {
       case P: 
          fmt.Println("This prints") 
    }
When no value is specified after "switch", the value of "true" is implied, which is why you can do:

    switch {
       case a && b: ..
       case something():
    }


No idea as I wouldn't do it :) (sorry - cop out that)




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: