M
M
ML2017-04-15 10:50:43
go
ML, 2017-04-15 10:50:43

How to work with switch golang?

Why does this construction not work in GO:

switch type {
    case "":
    case "football":
                log.Print(1)
    break
                case "basketball":
                log.Print(2)
                break
  }

But this one works:
switch type {
    case "":
                log.Print(1)
    break
    case "football":
                log.Print(1)
    break
                case "basketball":
                log.Print(2)
                break
  }

That is, for some reason, code duplication is necessary.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
om1058, 2017-04-15
@staffID

Because there is no fallthrough in the goshny switch. This is how it will be without duplication:

switch type {
    case "", "football":
        log.Print(1)
     case "basketball":
        log.Print(2)
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question