func main() {
f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
// 或者更简单的:
// return err
}
...
}
func main() {
...
if f, err = os.Open("filename.ext"); err != nil{
log.Fatal(err)
}
...
}
type error interface {
Error() string
}
buf := make([]byte, 100)
n, err := r.Read(buf)
buf = buf[:n]
if err == io.EOF {
log.Fatal("read failed:", err)
}
err := os.Remove("/tmp/nonexist")
log.Println(err)
2017/02/08 14:09:22 remove /tmp/nonexist: no such file or directory
// PathError records an error and the operation and file path that caused it.
type PathError struct {
Op string
Path string
Err error
}
func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
// file_unix.go 针对 *nix 系统的实现
// Remove removes the named file or directory.
// If there is an error, it will be of type *PathError.
func Remove(name string) error {
// System call interface forces us to know
// whether name is a file or directory.
// Try both: it is cheaper on average than
// doing a Stat plus the right one.
e := syscall.Unlink(name)
if e == nil {
return nil
}
e1 := syscall.Rmdir(name)
if e1 == nil {
return nil
}
// Both failed: figure out which error to return.
// OS X and Linux differ on whether unlink(dir)
// returns EISDIR, so can't use that. However,
// both agree that rmdir(file) returns ENOTDIR,
// so we can use that to decide which error is real.
// Rmdir might also return ENOTDIR if given a bad
// file path, like /etc/passwd/foo, but in that case,
// both errors will be ENOTDIR, so it's okay to
// use the error from unlink.
if e1 != syscall.ENOTDIR {
e = e1
}
return &PathError{"remove", name, e}
}
err := xxxx()
if err != nil {
swtich err := err.(type) {
case *os.PathError:
...
default:
...
}
}
err := errors.New("whoops")
// or
err := errors.Errorf("whoops: %s", "foo")
cause := errors.New("whoops")
err := errors.Wrap(cause, "oh noes")
err := errors.New("whoops")
fmt.Printf("%+v", err)
type temporary interface {
Temporary() bool
}
// IsTemporary returns true if err is temporary.
func IsTemporary(err error) bool {
te, ok := errors.Cause(err).(temporary)
return ok && te.Temporary()
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有