// lab02 project lab02.go package main import "fmt" func main() { xq1 := 123 Ft := 456 i := 0 i++ var tr, a, Ch = false, "stud", "" var ch rune ch = 'D' tr = 3 <= 4 const ( st = iota rr hn ) const su = iota const ar = iota const q1, q2, q3, q4 = 23.4, "asdf", 3 > 5, true const ( c = 20 d = 30 ) var ( x int = 10 y float64 = 3.14 z int e int ) x = x + c y = y + float64(c) z = x + int(y) // 需要将 float64 类型的 y 转换为 int 类型 var w1, w2 int = 17, 34 var w3, w4 int w3 = w1 % 3 w4 = w2 % 5 var f, g, h, j = 11, 25, 36, 14 tr = g+h<<2 < h-21 || !(f+8 > h) && (g>>1 < j) fmt.Printf("%d,%c,%s,%d,%d,%d,%f\n", tr, ch, Ch, st, rr, su, ar) fmt.Println(x, y, z) fmt.Println(w1, w2, w3, w4) fmt.Println(q1, q2, q3, q4) }运行报错# command-line-arguments .\test.go:130:2: xq1 declared and not used .\test.go:131:2: Ft declared and not used .\test.go:134:10: a declared and not used .\test.go:155:3: e declared and not used如何解决
时间: 2023-11-11 11:06:42 浏览: 165
可以通过删除未使用的变量或者给它们赋值来解决这些错误。
例如,删除未使用的变量 xq1 和 Ft:
```
package main
import "fmt"
func main() {
i := 0
i++
var tr, a, Ch = false, "stud", ""
var ch rune
ch = 'D'
tr = 3 <= 4
const (
st = iota
rr
hn
)
const su = iota
const ar = iota
const q1, q2, q3, q4 = 23.4, "asdf", 3 > 5, true
const (
c = 20
d = 30
)
var (
x int = 10
y float64 = 3.14
z int
e int
)
x = x + c
y = y + float64(c)
z = x + int(y) // 需要将 float64 类型的 y 转换为 int 类型
var w1, w2 int = 17, 34
var w3, w4 int
w3 = w1 % 3
w4 = w2 % 5
var f, g, h, j = 11, 25, 36, 14
tr = g + h<<2 < h - 21 || !(f + 8 > h) && (g>>1 < j)
fmt.Printf("%d,%c,%s,%d,%d,%d,%f\n", tr, ch, Ch, st, rr, su, ar)
fmt.Println(x, y, z)
fmt.Println(w1, w2, w3, w4)
fmt.Println(q1, q2, q3, q4)
}
```
或者给它们赋值:
```
package main
import "fmt"
func main() {
xq1 := 123
Ft := 456
i := 0
i++
var tr, a, Ch = false, "stud", ""
var ch rune
ch = 'D'
tr = 3 <= 4
const (
st = iota
rr
hn
)
const su = iota
const ar = iota
const q1, q2, q3, q4 = 23.4, "asdf", 3 > 5, true
const (
c = 20
d = 30
)
var (
x int = 10
y float64 = 3.14
z int
e int
)
x = x + c
y = y + float64(c)
z = x + int(y) // 需要将 float64 类型的 y 转换为 int 类型
var w1, w2 int = 17, 34
var w3, w4 int
w3 = w1 % 3
w4 = w2 % 5
var f, g, h, j = 11, 25, 36, 14
tr = g + h<<2 < h - 21 || !(f + 8 > h) && (g>>1 < j)
fmt.Printf("%d,%c,%s,%d,%d,%d,%f\n", tr, ch, Ch, st, rr, su, ar)
fmt.Println(x, y, z)
fmt.Println(w1, w2, w3, w4)
fmt.Println(q1, q2, q3, q4)
fmt.Println(xq1, Ft, a, e)
}
```
阅读全文