其他分享
首页 > 其他分享> > [Go] Error

[Go] Error

作者:互联网

package main

import (
	"errors"
	"fmt"
	"strings"
)

func shouldGreaterThanTen(num int) error {
	if num < 10 {
		return errors.New("Number is less than 10")
	}

	return nil // should return nil if no error
}

func openFile() error {
    f, err = os.Open("missingFile.txt")
    if err != nil {
     return err
    }
    
    defer f.Close()
    return nil
}

func main() {

	num := 9
	if err := shouldGreaterThanTen(num); err != nil {
		fmt.Println(fmt.Errorf("%d is Not greater than TEN", num))
		// panic(err) // this will throw error, stop the program
	}
    
    if err := openFile(); err != nil {
        fmt.Println(fmt.Errorf("%v", err))
    }
}

 

标签:return,err,nil,fmt,error,num,Error,Go
来源: https://www.cnblogs.com/Answer1215/p/16660301.html