{"id":1243,"date":"2022-11-29T09:31:04","date_gmt":"2022-11-29T01:31:04","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1243"},"modified":"2022-12-05T11:26:20","modified_gmt":"2022-12-05T03:26:20","slug":"go%e6%8c%87%e5%8d%97%e7%bb%83%e4%b9%a06-%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1243","title":{"rendered":"\u00a0Go\u6307\u5357\u7ec3\u4e606-\u9519\u8bef"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/go.dev\/tour\/list\" target=\"_blank\">A Tour of Go<\/a>\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u516d\u4e2a\u7ec3\u4e60\uff0c<a rel=\"noreferrer noopener\" href=\"https:\/\/tour.go-zh.org\/methods\/20\" target=\"_blank\">\u539f\u9898<\/a>\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4ece<a href=\"https:\/\/tour.go-zh.org\/flowcontrol\/8\" target=\"_blank\" rel=\"noreferrer noopener\">\u4e4b\u524d\u7684\u7ec3\u4e60<\/a>\u4e2d\u590d\u5236&nbsp;<code>Sqrt<\/code>&nbsp;\u51fd\u6570\uff0c\u4fee\u6539\u5b83\u4f7f\u5176\u8fd4\u56de&nbsp;<code>error<\/code>&nbsp;\u503c\u3002<\/p>\n\n\n\n<p><code>Sqrt<\/code>&nbsp;\u63a5\u53d7\u5230\u4e00\u4e2a\u8d1f\u6570\u65f6\uff0c\u5e94\u5f53\u8fd4\u56de\u4e00\u4e2a\u975e nil \u7684\u9519\u8bef\u503c\u3002\u590d\u6570\u540c\u6837\u4e5f\u4e0d\u88ab\u652f\u6301\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">type ErrNegativeSqrt float64<\/pre>\n\n\n\n<p>\u5e76\u4e3a\u5176\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func (e ErrNegativeSqrt) Error() string<\/pre>\n\n\n\n<p>\u65b9\u6cd5\u4f7f\u5176\u62e5\u6709&nbsp;<code>error<\/code>&nbsp;\u503c\uff0c\u901a\u8fc7&nbsp;<code>ErrNegativeSqrt(-2).Error()<\/code>&nbsp;\u8c03\u7528\u8be5\u65b9\u6cd5\u5e94\u8fd4\u56de&nbsp;<code>\"cannot Sqrt negative number: -2\"<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f:<\/strong>&nbsp;\u5728&nbsp;<code>Error<\/code>&nbsp;\u65b9\u6cd5\u5185\u8c03\u7528&nbsp;<code>fmt.Sprint(e)<\/code>&nbsp;\u4f1a\u8ba9\u7a0b\u5e8f\u9677\u5165\u6b7b\u5faa\u73af\u3002\u53ef\u4ee5\u901a\u8fc7\u5148\u8f6c\u6362&nbsp;<code>e<\/code>&nbsp;\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff1a<code>fmt.Sprint(float64(e))<\/code>\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p>\u4fee\u6539&nbsp;<code>Sqrt<\/code>&nbsp;\u51fd\u6570\uff0c\u4f7f\u5176\u63a5\u53d7\u4e00\u4e2a\u8d1f\u6570\u65f6\uff0c\u8fd4\u56de&nbsp;<code>ErrNegativeSqrt<\/code>&nbsp;\u503c\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u6211\u4eec\u53ea\u9700\u8981\u4fee\u6539\u4e00\u4e0b\u4e4b\u524d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u52a0\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\uff0c\u5f53\u5c0f\u4e8e0\u65f6\u8c03\u7528\u4e00\u4e0b\u9519\u8bef\u51fd\u6570\uff0c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u5373\u53ef\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n)\n\ntype ErrNegativeSqrt float64\n\nfunc (e ErrNegativeSqrt) Error() string {\n\treturn fmt.Sprintf(\"cannot Sqrt negative number: %g\", e)\n}\n\nfunc Sqrt(x float64) (float64, error) {\n\tif x &lt; 0 {\n\t\treturn x, ErrNegativeSqrt(x)\n\t}\n\t\n\tz := 1.0\n\tfor i := 0; i &lt; 10; i++ {\n\t\tz -= (z*z -x) \/ (2*z)\n\t}\n\treturn z, nil\n}\n\nfunc main() {\n\tfmt.Println(Sqrt(0))\n\tfmt.Println(Sqrt(-2.2))\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662fA Tour of Go\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u516d\u4e2a\u7ec3\u4e60\uff0c\u539f\u9898\u5982\u4e0b\uff1a \u4ece\u4e4b\u524d\u7684\u7ec3\u4e60\u4e2d\u590d\u5236&nbsp;Sqrt&nbsp;\u51fd\u6570\uff0c\u4fee\u6539\u5b83\u4f7f\u5176\u8fd4\u56de&nbsp;error&nbsp;\u503c\u3002 Sqrt&nbsp;\u63a5\u53d7\u5230\u4e00\u4e2a\u8d1f\u6570\u65f6\uff0c\u5e94\u5f53\u8fd4\u56de\u4e00\u4e2a\u975e nil \u7684\u9519\u8bef\u503c\u3002\u590d\u6570\u540c\u6837\u4e5f\u4e0d\u88ab\u652f\u6301\u3002 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b type ErrNegativeSqrt float64 \u5e76\u4e3a\u5176\u5b9e\u73b0 func (e ErrNegativeSqrt) Error() string \u65b9\u6cd5\u4f7f\u5176\u62e5\u6709&nbsp;error&nbsp;\u503c\uff0c\u901a\u8fc7&nbsp;ErrNegativeSqrt(-2).Error()&nbsp;\u8c03\u7528\u8be5\u65b9\u6cd5\u5e94\u8fd4\u56de&nbsp;&#8220;cannot Sqrt negative number: -2&#8243;\u3002 \u6ce8\u610f:&nbsp;\u5728&nbsp;Error&nbsp;\u65b9\u6cd5\u5185\u8c03\u7528&nbsp;fmt.Sprint(e)&nbsp;\u4f1a\u8ba9\u7a0b\u5e8f\u9677\u5165\u6b7b\u5faa\u73af\u3002\u53ef\u4ee5\u901a\u8fc7\u5148\u8f6c\u6362&nbsp;e&nbsp;\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff1afmt.Sprint(float64(e))\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f \u4fee\u6539&nbsp;Sqrt&nbsp;\u51fd\u6570\uff0c\u4f7f\u5176\u63a5\u53d7\u4e00\u4e2a\u8d1f\u6570\u65f6\uff0c\u8fd4\u56de&nbsp;ErrNegativeSqrt&nbsp;\u503c\u3002 \u6211\u4eec\u53ea\u9700\u8981\u4fee\u6539\u4e00\u4e0b\u4e4b\u524d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u52a0\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\uff0c\u5f53\u5c0f\u4e8e0\u65f6\u8c03\u7528\u4e00\u4e0b\u9519\u8bef\u51fd\u6570\uff0c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u5373\u53ef\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[374],"class_list":["post-1243","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":307,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1243"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}