I would like to declare a struct first and then initialize it inside a switch statement. The code I've written so far shows declared and not used errors. However, I think the problem is different in my case and related to scope of declaration.
Could somebody please help me to make the below code work?
Car.go
package main
import "fmt"
import "strconv"
type Car struct{
Name string
Price int
}
func main(){
name := "Fiat"
car := &Car{}
switch name {
case "Fiat":
car := &Car{
Name : "Fiat",
Price: 600000,
}
case "Mercedes-benz":
car := &Car{
Name : "Mercedes-benz",
Price: 5600000,
}
default:
car := &Car{
Name : "Toyota",
Price: 1000000,
}
}
fmt.Println("Car Name : " + car.Name + " Price : " + strconv.Itoa(car.Price));
}
Errors
$go run Car.go
./Car.go:19: car declared and not used
./Car.go:24: car declared and not used
./Car.go:29: car declared and not used