Can someone help me in below code, I have used both if and switch to compare string in golang but both of them are not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me in below code, I have used both if and switch to compare string in golang but both of them are not working

package main import "fmt" //import "strings" func main() { results := []string{"w", "l", "w", "d", "w", "l", "l", "l", "d", "d", "w", "l", "w", "d"} var x string fmt.Scanln(x) a := "w" b := "d" /* if strings.Compare(x,a) == 0{ fmt.Println(22) }else if x == b { fmt.Println(20) }else if x == "l"{ fmt.Println(19) } */ switch x{ case "w": fmt.Println(22) case "d": fmt.Println(20) default : fmt.Println(19) } results = append(results ,x) }

17th Aug 2021, 2:05 PM
Muhammad Owais Mushtaq
Muhammad Owais Mushtaq - avatar
1 Answer
+ 4
You missed to return the address of your input variable. fmt.Scanln(&x) It's not necessary to declare variables if you don't want to assign them. //a := "w" //b := :d" or if you want to use them switch x{ case a: fmt.Println(22) case b: fmt.Println(20) default : fmt.Println(19) }
17th Aug 2021, 4:30 PM
Simba
Simba - avatar