-2
我想首先聲明一個結構,然後在switch語句中初始化它。我迄今爲止寫的代碼顯示了declared and not used錯誤。但是,我認爲這個問題與我的情況不同,與申報範圍有關。如何在switch語句中首先聲明結構並啓動它?
有人請幫我做下面的代碼工作嗎?
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
我知道問題出在範圍之內。感謝您用正確的語法幫助我:) – Beginner
沒問題。我們都曾經是學習者。 –