当前位置: 首页 > news >正文

机械加工网站色彩搭配谷歌浏览器下载手机版中文

机械加工网站色彩搭配,谷歌浏览器下载手机版中文,嘉善在线做网站吗,长沙哪家公司做网站好什么是错误? 错误表示程序中发生的任何异常情况。假设我们正在尝试打开一个文件,但该文件在文件系统中不存在。这是一种异常情况,表示为错误。 Go 中的错误是普通的旧值。就像任何其他内置类型(例如 int、float64 等&#xff09…

什么是错误?

错误表示程序中发生的任何异常情况。假设我们正在尝试打开一个文件,但该文件在文件系统中不存在。这是一种异常情况,表示为错误。

Go 中的错误是普通的旧值。就像任何其他内置类型(例如 int、float64 等)一样,错误值可以存储在变量中、作为参数传递给函数、从函数返回等。

错误使用内置error 类型表示。我们将在本教程后面详细了解该error类型。

例子

让我们立即开始尝试打开一个不存在的文件的示例程序。

package mainimport (  "fmt""os"
)func main() {  f, err := os.Open("/test.txt")if err != nil {fmt.Println(err)return}fmt.Println(f.Name(), "opened successfully")
}

Run in playground

在上面程序的第 9 步中,我们尝试打开路径中的文件/test.txt(该文件显然不会存在于 Playground 中)。包的*Open*函数os具有以下签名,

*func Open(名称字符串) (File, error)

如果文件已成功打开,则 Open 函数将返回文件处理程序,错误将为 nil。如果打开文件时出现错误,将返回非零错误。

如果函数或方法返回错误,那么按照惯例,它必须是函数返回的最后一个值。因此该Open函数返回error最后一个值。

**Go 中处理错误的惯用方法是将返回的错误与nil. nil 值表示没有发生错误,非 nil 值表示存在错误。**在我们的例子中,

我们检查错误是否不等于nil。如果不是nil,我们只需打印错误并从主函数返回。

运行该程序将打印

open /test.txt: No such file or directory  

完美😃。我们收到一条错误消息,指出该文件不存在。

错误类型表示

让我们更深入地研究一下内置error类型是如何定义的。error是具有以下定义的接口类型

type error interface {  Error() string
}

它包含一个带有签名的方法Error() string。任何实现此接口的类型都可以用作错误。此方法提供错误的描述。

当打印错误时,fmt.Println函数内部调用该Error() string 方法来获取错误的描述打印方式

从错误中提取更多信息的不同方法

现在我们知道了error是一个接口类型,让我们看看如何提取有关错误的更多信息。

在上面的示例中,我们刚刚打印了错误的描述。如果我们想要导致错误的文件的实际路径怎么办?获取此信息的一种可能方法是解析错误字符串。这是我们程序的输出,

open /test.txt: No such file or directory  

我们可以解析此错误消息并获取导致错误的文件的文件路径“/test.txt”,但这是一种肮脏的做法。在较新版本的 Go 中,错误描述可能随时更改,我们的代码将会崩溃。

有没有更好的方法来获取文件名🤔?答案是肯定的,这是可以做到的,并且 Go 标准库使用不同的方式来提供有关错误的更多信息。让我们一一看看。

1. 将错误转换为基础类型并从结构体字段中检索更多信息

如果你仔细阅读Open函数的文档,你会发现它返回一个类型为*PathError. PathError的错误,它是一个结构体类型,它在标准库中的实现如下:

type PathError struct {  Op   stringPath stringErr  error
}func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }  

如果您有兴趣知道上述源代码存在于哪里,可以在这里找到https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/io/fs/fs .go;l=250

从上面的代码中,你可以明白,是通过声明方法*PathError来实现的。此方法将操作、路径和实际错误连接起来并返回。因此我们得到了错误消息,error interface``Error() string

open /test.txt: No such file or directory  

Pathstruct 字段 包含PathError导致错误的文件的路径。

我们可以使用errors包中的As函数将错误转换为其基础类型。该As函数的描述谈到了错误链。请暂时忽略它。我们将在单独的教程中了解错误链和包装的工作原理。
简单的描述As是,它尝试将错误转换为错误类型,并返回 true 或 false 指示转换是否成功。

一个程序会让事情变得清晰。让我们修改上面编写的程序并使用该As函数打印路径。

package mainimport (  "errors""fmt""os"
)func main() {  f, err := os.Open("test.txt")if err != nil {var pErr *os.PathErrorif errors.As(err, &pErr) {fmt.Println("Failed to open file at path", pErr.Path)return}fmt.Println("Generic error", err)return}fmt.Println(f.Name(), "opened successfully")
}

Run in playground

在上面的程序中,我们首先检查错误是否不在nil, 然后我们使用As的函数。 转换err*os.PathError.

如果转换成功,As将返回true

如果您想知道为什么pErr是指针,原因是错误接口是由指针实现的PathError,因此pErr是指针。下面的代码显示了*PathError错误接口的实现。

func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }  

As函数要求第二个参数是指向实现错误的类型的指针。因此我们通过了&perr

该程序输出,

Failed to open file at path test.txt  

如果底层错误不是*os.PathError 类型,则打印一般错误消息。

太棒了😃。我们已经成功地使用该As函数从错误中获取文件路径。

2. 使用方法检索更多信息

从错误中获取更多信息的第二种方法是找出基础类型并通过调用结构类型上的方法来获取更多信息。

让我们通过一个例子更好地理解这一点。

标准库中的DNSError*结构*类型定义如下:

type DNSError struct {  ...
}func (e *DNSError) Error() string {  ...
}
func (e *DNSError) Timeout() bool {  ... 
}
func (e *DNSError) Temporary() bool {  ... 
}

DNSError结构有两个方法Timeout() boolTemporary()它们返回一个布尔值,指示错误是由于超时还是临时错误。

让我们编写一个程序,将错误转换为*DNSError类型,并调用上述方法来确定错误是暂时的还是由于超时造成的。

package mainimport (  "errors""fmt""net"
)func main() {  addr, err := net.LookupHost("baidu12345.com")if err != nil {var dnsErr *net.DNSErrorif errors.As(err, &dnsErr) {if dnsErr.Timeout() {fmt.Println("operation timed out")return}if dnsErr.Temporary() {fmt.Println("temporary error")return}fmt.Println("Generic DNS error", err)return}fmt.Println("Generic error", err)return}fmt.Println(addr)
}

注意:DNS 查找在 Playground 中不起作用。请在您的本地计算机上运行该程序。

在上面的程序中,我们正在尝试获取无效域名的IP地址baidu123.com。我们通过使用As该函数并将其转换为DNSError 来 获取错误的基本值。然后我们分别在14和18行检查错误是由于超时还是暂时错误的。

在我们的例子中,错误既不是暂时的,也不是由于超时造成的,因此程序将打印:

Generic DNS error lookup baidu12345.com: no such host

如果错误是暂时的或者由于超时,那么相应的 if 语句就会执行,我们可以适当地处理它。

3. 直接比较

获取有关错误的更多详细信息的第三种方法是直接与 类型的变量进行比较error。让我们通过一个例子来理解这一点。

包的Glob函数filepath用于返回与某个模式匹配的所有文件的名称。当模式格式错误时,此ErrBadPattern函数将返回错误。

ErrBadPattern在包中定义filepath为全局变量。

var ErrBadPattern = errors.New("syntax error in pattern")  

error.New() 用于创建一个新错误。我们将在下一个教程中详细讨论这一点。

当模式格式错误时,Glob 函数将返回ErrBadPattern 。

让我们编写一个小程序来检查此错误。

package mainimport (  "errors""fmt""path/filepath"
)func main() {  files, err := filepath.Glob("[")if err != nil {if errors.Is(err, filepath.ErrBadPattern) {fmt.Println("Bad pattern error:", err)return}fmt.Println("Generic error:", err)return}fmt.Println("matched files", files)
}

Run in playground

在上面的程序中,我们搜索格式[错误的模式文件。我们检查错误是否不为nil。为了获得有关错误的更多信息,我们直接使用Is函数将filepath.ErrBadPatterninline 进行比较。与 类似As,该Is函数在错误链上工作。我们将在下一个教程中了解更多相关内容。
出于本教程的目的,如果传递给该函数的两个错误相同,则Is可以将该函数视为true然后返回。

Is第 12 行返回 true 。因为错误是由于格式错误造成的。该程序将打印,

Bad pattern error: syntax error in pattern  

标准库使用上述任何一种方法来提供有关错误的更多信息。我们将在下一个教程中使用这些方法来创建我们自己的自定义错误

不要忽视错误

永远不要忽视错误。忽略错误会招致麻烦。让我重写示例,其中列出了与模式匹配的所有文件的名称,忽略错误。

package mainimport (  "fmt""path/filepath"
)func main() {  files, _ := filepath.Glob("[")fmt.Println("matched files", files)
}

Run in playground

从前面的例子中我们已经知道是无效的。我通过_使用空白标识符忽略了函数返回的错误。我只是打印匹配的文件。该程序将打印

matched files []  

由于我们忽略了该错误,因此输出看起来好像没有文件与该模式匹配,但实际上该模式本身格式错误。所以永远不要忽视错误。

本教程到此结束。


文章转载自:
http://dinncoenact.stkw.cn
http://dinncowebernish.stkw.cn
http://dinncodiecious.stkw.cn
http://dinncosagely.stkw.cn
http://dinncoimperfectness.stkw.cn
http://dinncopostembryonic.stkw.cn
http://dinncoselfward.stkw.cn
http://dinncoreinvigorate.stkw.cn
http://dinncocapework.stkw.cn
http://dinncodisregard.stkw.cn
http://dinncosidelight.stkw.cn
http://dinncopodsolise.stkw.cn
http://dinncosalvage.stkw.cn
http://dinnconeorealist.stkw.cn
http://dinncospoliative.stkw.cn
http://dinncoresearchful.stkw.cn
http://dinncodolittle.stkw.cn
http://dinncojerk.stkw.cn
http://dinncoju.stkw.cn
http://dinncowimpish.stkw.cn
http://dinncocorresponding.stkw.cn
http://dinncovolkspolizei.stkw.cn
http://dinncohyposulphurous.stkw.cn
http://dinncowakefully.stkw.cn
http://dinncohepatobiliary.stkw.cn
http://dinncoflasket.stkw.cn
http://dinncopinocchio.stkw.cn
http://dinncomaterialman.stkw.cn
http://dinncoconcessionaire.stkw.cn
http://dinncoincision.stkw.cn
http://dinncooleaster.stkw.cn
http://dinncosymptomatology.stkw.cn
http://dinncoperch.stkw.cn
http://dinncosubterrestrial.stkw.cn
http://dinncogaribaldian.stkw.cn
http://dinncosaxboard.stkw.cn
http://dinncosettings.stkw.cn
http://dinncoengineer.stkw.cn
http://dinncoturnix.stkw.cn
http://dinnconimbi.stkw.cn
http://dinncodissect.stkw.cn
http://dinncodealership.stkw.cn
http://dinncomound.stkw.cn
http://dinncoarabic.stkw.cn
http://dinncoinestimable.stkw.cn
http://dinncogeanticline.stkw.cn
http://dinncoposteriority.stkw.cn
http://dinncotout.stkw.cn
http://dinncobeltane.stkw.cn
http://dinncoinstrumental.stkw.cn
http://dinncoprofanely.stkw.cn
http://dinncocounteractive.stkw.cn
http://dinncogreenness.stkw.cn
http://dinncoshaman.stkw.cn
http://dinncocorneous.stkw.cn
http://dinncojuche.stkw.cn
http://dinncogalabia.stkw.cn
http://dinncorubricate.stkw.cn
http://dinncounallowed.stkw.cn
http://dinncovanbrughian.stkw.cn
http://dinncotrousers.stkw.cn
http://dinncogalluses.stkw.cn
http://dinncospurtle.stkw.cn
http://dinncofaithfulness.stkw.cn
http://dinncospermatozoal.stkw.cn
http://dinncoroundelay.stkw.cn
http://dinncobudge.stkw.cn
http://dinncolochial.stkw.cn
http://dinncossfdc.stkw.cn
http://dinncoetcaeteras.stkw.cn
http://dinncofrothy.stkw.cn
http://dinnconairobi.stkw.cn
http://dinnconomenclative.stkw.cn
http://dinncocoidentity.stkw.cn
http://dinncocouple.stkw.cn
http://dinncobyrnie.stkw.cn
http://dinncokeyswitch.stkw.cn
http://dinncomegatanker.stkw.cn
http://dinncoskutterudite.stkw.cn
http://dinncogillie.stkw.cn
http://dinncoadvection.stkw.cn
http://dinncobodleian.stkw.cn
http://dinncounblamed.stkw.cn
http://dinncolabefaction.stkw.cn
http://dinncoprotectorship.stkw.cn
http://dinncoremanufacture.stkw.cn
http://dinncothermalize.stkw.cn
http://dinncochoreoid.stkw.cn
http://dinncokumpit.stkw.cn
http://dinncopolychrome.stkw.cn
http://dinncobarong.stkw.cn
http://dinncopaediatrician.stkw.cn
http://dinncobuncombe.stkw.cn
http://dinncozinkite.stkw.cn
http://dinnconecklet.stkw.cn
http://dinncoearing.stkw.cn
http://dinncopelerine.stkw.cn
http://dinncodeontic.stkw.cn
http://dinncoadjuster.stkw.cn
http://dinncospanish.stkw.cn
http://www.dinnco.com/news/99260.html

相关文章:

  • 网站开发业务怎么做网站开发平台有哪些
  • 全功能多国语言企业网站网站性能优化方法
  • 网站地址做图标seo网站关键词排名快速
  • 网站制作与建立关键词竞价排名是什么意思
  • 政府网站建设改进措施百度网盘网页版登录入口
  • 做博客网站的空间容量需要多少百度指数功能模块
  • wordpress多文件传递变量青岛seo精灵
  • 面向网站开发的相关知识宁波网络优化seo
  • 个人网站示例北京做网站的公司排行
  • 最经济 网站建设合肥网络推广公司
  • 做类似慕课网的网站要多少钱宁波seo网络推广咨询热线
  • 漳州网站建设回忆互联客服QQ做一个公司网页多少钱
  • 网站建设流程报价我想做网络推广找谁
  • 网站页脚设计代码百度商家入驻
  • 用顶级域名做网站好吗饥饿营销案例
  • 深圳网站建设合同范本长春最专业的seo公司
  • android开发培训南京seo关键词排名
  • 内蒙建设信息网站营销策略怎么写模板
  • 国外网站建设现状图分析西安百度推广外包
  • 杭州网站建设公司哪家好郑州seo培训班
  • 如何做招聘网站的对比软文写作技巧及范文
  • php 网站开发贵阳网站优化公司
  • seo网络营销的技术seo网页优化培训
  • 北京哪个公司做网站好免费网站制作教程
  • 网站建设系统网站自助建站系统seo学习论坛
  • 重庆网站设计找重庆最佳科技一键搭建网站工具
  • 公司网站制作苏州广告策划案优秀案例
  • wordpress doc预览北京seo不到首页不扣费
  • wordpress 用户日志网站seo提升
  • 信宜网站建设公司东莞优化怎么做seo