Can I use ... in switch? (example inside)

142 views
Skip to first unread message

Kevin P

unread,
Jul 26, 2016, 11:02:33 PM7/26/16
to golang-nuts
Is this valid?


package main

import (
"fmt"
)

func main() {
a := []int{0,1,2,3,4,5}
switch 1 {
case a...:
 fmt.Println("match")
default:
  fmt.Println("no match")
}
}

Val

unread,
Jul 27, 2016, 4:03:37 AM7/27/16
to golang-nuts
Like the Playground probably told you ... no.
What are you trying to achieve?
There are several valid ways to use switch, depending whether each case is a condition, or a value, or a type.
Cheers
Val

Val

unread,
Jul 27, 2016, 4:12:36 AM7/27/16
to golang-nuts
Oh, I just understood after posting, sorry : "case a..." would mean "is 1 an element of a", just like if we had written "case 0, 1, 2, 3, 4, 5".
It is still afaik not an allowed syntax.
To achieve something equivalent, I would have to use a helper func : https://play.golang.org/p/IUqQKjKuHb
Reply all
Reply to author
Forward
0 new messages