<分区>
我想先声明一个结构,然后在 switch 语句中初始化它。到目前为止我编写的代码显示 Improve this question 错误。但是,我认为问题与我的情况有所不同,并且与声明范围有关。
有人可以帮我使下面的代码工作吗?
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));
}
错误
$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