文件或目录的通用变量名是"path“。不幸的是,这也是Go中的一个包的名称。此外,在DoIt中将path更改为参数名,如何编译此代码?
package main
import (
"path"
"os"
)
func main() {
DoIt("file.txt")
}
func DoIt(path string) {
path.Join(os.TempDir(), path)
}我得到的错误是:
$6g pathvar.go
pathvar.go:4: imported and not used: path
pathvar.go:13: path.Join undefined (type string has no field or method Join)发布于 2011-10-15 03:03:56
path string正在隐藏导入的path。您可以通过将import中的行"path"更改为pathpkg "path"来将导入包的别名设置为例如pathpkg,因此代码的开头如下所示
package main
import (
pathpkg "path"
"os"
)当然,然后您必须将DoIt代码更改为:
pathpkg.Join(os.TempDir(), path)发布于 2011-10-15 16:07:32
package main
import (
"path"
"os"
)
func main() {
DoIt("file.txt")
}
// Just don't introduce a same named thing in the scope
// where you need to use the package qualifier.
func DoIt(pth string) {
path.Join(os.TempDir(), pth)
}https://stackoverflow.com/questions/7772229
复制相似问题