Switch Question (sorry so simple)

My question is why does this work:

switch {
    case cfg.PlayNew.TreeReportingOptions.Type == "very narrow":
        treeMoveWidth = 1
    case cfg.PlayNew.TreeReportingOptions.Type == "narrow":
        treeMoveWidth = 3
    case cfg.PlayNew.TreeReportingOptions.Type == "regular":
        treeMoveWidth = 8

    }

BUT this does not??? Note: cfg.PlayNew.TreeReportingOptions.Type is a string

switch cfg.PlayNew.TreeReportingOptions.Type {
    case "very narrow":
        treeMoveWidth = 1
    case "narrow":
        treeMoveWidth = 3
    case "regular":
        treeMoveWidth = 8

    }

Thank you for your time!

The second example should be working fine. This:

func main() {
	treeMoveWidth := 0
	treeReportingOptionsType := "narrow"
	switch treeReportingOptionsType {
	case "very narrow":
		treeMoveWidth = 1
	case "narrow":
		treeMoveWidth = 3
	case "regular":
		treeMoveWidth = 8
	}
	fmt.Println("treeMoveWidth =", treeMoveWidth)
}

… prints the following:

treeMoveWidth = 3

You can run it yourself on the playground.

4 Likes

Thanks turned out there was an unprintable character in the code so that is why it would not compile.

1 Like