- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
PathError
在 Golang 的 os
库中找到的类型:
type PathError struct {
Op string
Path string
Err error
}
func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
几乎 实现了 Go 的 error
interface :
type error interface {
Error() string
}
但是,当尝试将其作为错误传递时,您会收到以下编译时错误:
cannot use (type os.PathError) as type error in argument...
os.PathError does not implement error (Error method has pointer receiver)
为什么 os.PathError
会为 Error 方法使用指针接收器,而只是避免满足错误接口(interface)的要求?
完整示例:
package main
import (
"fmt"
"os"
)
func main() {
e := os.PathError{Path: "/"}
printError(e)
}
func printError(e error) {
fmt.Println(e)
}
最佳答案
在此处阅读有关方法集的信息:https://golang.org/ref/spec#Method_sets
The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T)
您正在尝试使用 os.PathError
类型调用采用 error
接口(interface)的函数。根据上述,它没有实现 Error() string
,因为该方法是在 *os.PathError
类型上定义的。
有了 os.PathError
,您可以使用 &
运算符得到 *os.PathError
:
printError(&os.PathError{...})
关于go - os.PathError 没有实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051486/
我正在使用 glom 包来帮助遍历一个大字典。 鉴于此数据: data = { 'groups': [ {'instance': {'name': 'football'}},
PathError在 Golang 的 os 库中找到的类型: type PathError struct { Op string Path string Err err
我正在尝试确定 os.PathError 是由 EINVAL 还是 ENOENT 引起的。我如何正确地做出该决定? res, err := os.Readlink(fpath) if
当我阅读时:http://golang.org/doc/effective_go.html#errors 我发现这样一行:err.(*os.PathError) in this context: fo
部署此 Go-based AWS Lambda project 时,通过 AWS 控制台,我收到: { "errorMessage": "fork/exec /var/task/main: exe
我在 Golang 中开发了一组 Lambda 函数,并尝试使用 SAM 部署这些函数和 API 网关。 我正在本地创建可执行文件,为每个 lambda 函数创建 zip 文件,并将这些 zip 文件
我是一名优秀的程序员,十分优秀!