Shortening the code - "switch" case

Hello everyone,
I’m writing a simple code for a board gaming purposes. It should draw two, three or four nations (depending on how many players are in) out of 16 possible ones. No repetitions.

Here is the key part:

					var a = rand.Intn(17-1)+1		

					switch a {				
						case 1: 
						case 2: 
						case 3: 
						fmt.Println("sand runners")
						case 4: 
						case 5: 
						case 6: 
						fmt.Println("nowy jork")
						case 7: 
						case 8: 
						case 9: 
						case 10: 
						case 11: 
						case 12: 
						fmt.Println("iron gang")
						case 13: 
						case 14: 
						case 15: 
						case 16: 

I used to have this block four times (because of the four draws). I’d like to shorten it; I’d like to have the above part of the code only once.
Is that possible to somehow change this part:

  			switch a {				

to something like this (I know, the syntax below is bad):

  			switch a, b, c, d {				

In other words: I would like the code to draw four players at once: player a, b, c ,d. Just from this “switch” block I’ve pasted here.
The code has also an input box (how many players are playing? [2-4]. Even if we choose 2 players, the code can draw 4 players - no matter, because the code will eventually print out as many outputs as many players we declared.

Is there a magical syntax that can help me? I think it’s messy to have 4x almost the same block of text in the code.


Is this what you are getting at? Fallthrough for your cases with common code:

case 1: fallthrough
case 2: fallthrough
case 3:
case 4: // etc.

Sorry, I skimmed the problem too coarsely and answered a different problem.

What you want is not related to the switch statement.

What you want is to create a single function that you call 4 times.

something like this?

package main

import (

func main() {
	rand.Seed(time.Now().UnixNano())  // change the seed manually in "The Go Playground"
	players := getFirstX(rand.Intn(4) + 1)
	for i := range players {

func getFirstX(X int) []string {
	data := []string{"uranopolis", "smart", "sand runners", "policja", "dancer"}
	rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] })
	return data[:X]

I’m not sure if anyone else’s response answered your question or not, but to me it looks like you want a for loop? Something like:

numberOfNations := askUserForNumberOfNations()

for i := 0; i < numberOfNations; i++ {
    nationCode := askUserForNationCode()
    a := nationCode
    switch a {
        case 1:
        // ...

Maybe something like this:

package main

import "fmt"

var cities = map[int]string{
	0: "uranopolis", 1: "smart", 2: "sand runners", 3: "policja"}

func main() {

func GetNation(selectedNr int) string {
	if value, ok := cities[selectedNr]; ok {
		delete(cities, selectedNr)
		return value
	return ""

Thanks for your replies. I think @NobbZ is right. I think I should create a funcion that draws 1 out of 16 possible nations and put this part of code inside the “general” funciton, where - accordingly to the input data - “drawing” function will be called as many times as needed.
I’m a very beginner so I’m eager to test all your suggestions. Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.