【发布时间】:2021-01-19 19:25:31
【问题描述】:
我不明白如何在 go 中适当地重新分配块范围内的变量。
package main
import (
"fmt"
"path/filepath"
)
func main() {
base := "/a/b/c"
other := "/a/b/c/d/e"
for base != other {
other, file := filepath.Split(other) // "other declared but not used"
fmt.Println(file)
}
}
我想使用filepath.Split 的两个部分,所以我需要:=,因为file 尚未声明。我想让other越来越短,所以我重新分配了filepath.Split的结果,但是go编译器不允许我运行这段代码。
为什么会这样,我该怎么做?
【问题讨论】:
标签: go