{"id":4129,"date":"2023-10-18T20:00:02","date_gmt":"2023-10-18T11:00:02","guid":{"rendered":"https:\/\/dennie.tokyo\/it\/?p=4129"},"modified":"2025-07-10T19:22:17","modified_gmt":"2025-07-10T10:22:17","slug":"%e3%80%90go%e3%80%91%e3%82%a8%e3%83%a9%e3%83%bc%e5%87%a6%e7%90%86%e3%81%ae-is","status":"publish","type":"post","link":"https:\/\/dennie.tokyo\/it\/?p=4129","title":{"rendered":"\u3010Go\u3011\u30a8\u30e9\u30fc\u30e9\u30c3\u30d4\u30f3\u30b0\u3068\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc"},"content":{"rendered":"\n<p>\u524d\u56de\u306f\u30a8\u30e9\u30fc\u51e6\u7406\u306e\u57fa\u672c\u3092\u78ba\u8a8d\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u305d\u306e\u4e2d\u3067\u30a8\u30e9\u30fc\u306e\u30e9\u30c3\u30d4\u30f3\u30b0\u3068\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc\u306b\u3064\u3044\u3066\u3082\u7c21\u5358\u306b\u8aac\u660e\u3057\u307e\u3057\u305f\u3002\u4eca\u56de\u306f\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc\u306e\u30e9\u30c3\u30d4\u30f3\u30b0\u306b\u3064\u3044\u3066\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">fmt.Errorf\u95a2\u6570\u3067\u30a8\u30e9\u30fc\u306e\u30e9\u30c3\u30d4\u30f3\u30b0<\/h1>\n\n\n\n<p>\u524d\u56de\u306e\u30a8\u30e9\u30fc\u51e6\u7406\u306e\u57fa\u672c\u3067\u3082\u52d5\u4f5c\u3092\u78ba\u8a8d\u3057\u307e\u3057\u305f\u304c\u3001fmt.Errorf\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u307e\u3059\u3002errors.Is\u95a2\u6570\u306f\u7c21\u5358\u306b\u8a00\u3046\u3068\u53d7\u3051\u53d6\u3063\u305f\u30a8\u30e9\u30fc\u304c\u60f3\u5b9a\u3055\u308c\u308b\u30a8\u30e9\u30fc\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3057\u3066\u304f\u308c\u308b\u95a2\u6570\u3068\u601d\u3063\u3066\u4e0b\u3055\u3044\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>package main\n\nimport (\n\t&quot;errors&quot;\n\t&quot;fmt&quot;\n)\n\ntype MyError struct {\n}\n\nfunc (em MyError) Error() string {\n\treturn &quot;\u4ffa\u306e\u30a8\u30e9\u30fc\uff01\uff01&quot;\n}\n\nfunc main() {\n\t\/\/ \u30a8\u30e9\u30fc\u30e9\u30c3\u30d7\u3059\u308b\n\treErr := fmt.Errorf(&quot;%w&quot;, MyError{})\n\n\t\/\/ \u60f3\u5b9a\u901a\u308a\u306e\u30a8\u30e9\u30fc\u3067\u3042\u308b\u4e8b\u3092\u78ba\u8a8d\u3059\u308b\n\tif errors.Is(reErr, MyError{}) {\n\t\tfmt.Println(&quot;\u4ffa\u306e\u30a8\u30e9\u30fc\u304c\u3042\u3063\u305f\uff01\uff01&quot;)  \/\/ \u4ffa\u306e\u30a8\u30e9\u30fc\u304c\u3042\u3063\u305f\uff01\uff01 \u304c\u8868\u793a\u3055\u308c\u308b\u3002\n\t} else {\n\t\tfmt.Println(&quot;\u4ffa\u306e\u30a8\u30e9\u30fc\u306f\u306a\u3044\uff01\uff01&quot;)\n\t}\n}<\/code><\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\">\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc\u3067\u30e9\u30c3\u30d4\u30f3\u30b0<\/h1>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc\u306b\u4ed6\u306e\u30a8\u30e9\u30fc\u3092\u4fdd\u6301\u3057\u305f\u3044\u6642\u3082\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u7d50\u679c\u306f\u4fdd\u6301\u3057\u3066\u3044\u308b\u30a8\u30e9\u30fc\u3092\u78ba\u8a8d\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>package main\n\nimport (\n\t&quot;errors&quot;\n\t&quot;fmt&quot;\n)\n\ntype MyError struct {\n\terr error\n}\n\ntype orgError struct {\n}\n\nfunc (em MyError) Error() string {\n\treturn &quot;\u4ffa\u306e\u30a8\u30e9\u30fc\uff01\uff01&quot;\n}\n\nfunc (em orgError) Error() string {\n\treturn &quot;\u30aa\u30ea\u30b8\u30ca\u30eb\u30a8\u30e9\u30fc\uff01\uff01&quot;\n}\n\nfunc main() {\n\t\/\/ \u30aa\u30ea\u30b8\u30ca\u30eb\u30a8\u30e9\u30fc\u3092\u4ffa\u306e\u30a8\u30e9\u30fc\u3067\u30e9\u30c3\u30d4\u30f3\u30b0\u3059\u308b\n\treErr := MyError{err: orgError{}}\n\n\t\/\/ \u30aa\u30ea\u30b8\u30ca\u30eb\u306e\u30a8\u30e9\u30fc\u3092\u78ba\u8a8d\u3059\u308b\n\tif errors.Is(reErr, MyError{}) {\n\t\tfmt.Println(&quot;\u4ffa\u306e\u30a8\u30e9\u30fc\u304c\u3042\u3063\u305f\uff01\uff01&quot;)\n\t} else {\n\t\tfmt.Println(&quot;\u4ffa\u306e\u30a8\u30e9\u30fc\u306f\u306a\u3044\uff01\uff01&quot;) \/\/ \u4ffa\u306e\u30a8\u30e9\u30fc\u306f\u306a\u3044\uff01\uff01 \u304c\u8868\u793a\u3055\u308c\u308b\n\t}\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>fmt.Errorf\u95a2\u6570\u3067\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u305f\u30a8\u30e9\u30fc\u306ferrors.Is\u95a2\u6570\u3067\u691c\u77e5\u3067\u304d\u307e\u3057\u305f\u304c\u3001\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc\u3067\u30e9\u30c3\u30d4\u30f3\u30b0\u3057\u305f\u30a8\u30e9\u30fc\u306f\u691c\u77e5\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u6c7a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">errors.Is\u95a2\u6570\u306e\u5185\u5bb9<\/h3>\n\n\n\n<p>\u3056\u3063\u304f\u308a\u8a00\u3046\u3068errors.Is\u95a2\u6570\u306f\u7b2c\u4e00\u5f15\u6570\u306b\u4ed6\u304b\u3089\u53d7\u3051\u53d6\u3063\u305f\u30a8\u30e9\u30fc\u3001\u7b2c\u4e8c\u5f15\u6570\u306b\u60f3\u5b9a\u3059\u308b\u30a8\u30e9\u30fc\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002errors.Is\u95a2\u6570\u306f\u5185\u90e8\u3067\u7b2c\u4e00\u5f15\u6570\u306e\u30a8\u30e9\u30fc\u306b\u5bfe\u3057\u3066Unwrap\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3001\u7b2c\u4e8c\u5f15\u6570\u306e\u30a8\u30e9\u30fc\u3068\u6bd4\u8f03\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">fmt.Errorf\u95a2\u6570\u304c\u8fd4\u3059\u3082\u306e<\/h3>\n\n\n\n<p>fmt.Errorf\u95a2\u6570\u304c\u8fd4\u3059\u30a8\u30e9\u30fc\u306f\u3001Unwrap\u95a2\u6570\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u306a\u306e\u3067errors.Is\u95a2\u6570\u306f\u30e9\u30c3\u30d4\u30f3\u30b0\u3055\u308c\u305f\u30a8\u30e9\u30fc\u3092\u691c\u77e5\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5177\u4f53\u7684\u306a\u5bfe\u7b56<\/h2>\n\n\n\n<p>\u3068\u8a00\u3046\u3053\u3068\u3067errors.Is\u95a2\u6570\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u6e21\u3059\u30a8\u30e9\u30fc\u306bUnwrap\u95a2\u6570\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>func (em MyError) Unwrap() error {\n\treturn em.err\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u5168\u4f53\u7684\u306a\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>package main\n\nimport (\n\t&quot;errors&quot;\n\t&quot;fmt&quot;\n)\n\ntype MyError struct {\n\terr error\n}\n\ntype orgError struct {\n}\n\nfunc (em MyError) Error() string {\n\treturn &quot;\u4ffa\u306e\u30a8\u30e9\u30fc\uff01\uff01&quot;\n}\n\n\/\/ \u8ffd\u52a0\nfunc (em MyError) Unwrap() error {\n\treturn em.err\n}\n\nfunc (em orgError) Error() string {\n\treturn &quot;\u305d\u306e\u4ed6\u30a8\u30e9\u30fc\uff01\uff01&quot;\n}\n\nfunc main() {\n\t\/\/ \u30a8\u30e9\u30fc\u3092\u30e9\u30c3\u30d7\u3059\u308b\n\treErr := MyError{err: orgError{}}\n\n\t\/\/ \u30aa\u30ea\u30b8\u30ca\u30eb\u30a8\u30e9\u30fc\u306e\u5b58\u5728\u3092\u78ba\u8a8d\u3059\u308b\n\tif errors.Is(reErr, orgError{}) {\n\t\tfmt.Println(&quot;\u305d\u306e\u4ed6\u30a8\u30e9\u30fc\u304c\u3042\u3063\u305f\uff01\uff01&quot;) \/\/ \u305d\u306e\u4ed6\u30a8\u30e9\u30fc\u304c\u3042\u3063\u305f\uff01\uff01 \u304c\u8868\u793a\u3055\u308c\u308b\n\t} else {\n\t\tfmt.Println(&quot;\u305d\u306e\u4ed6\u30a8\u30e9\u30fc\u306f\u306a\u3044\uff01\uff01&quot;)\n\t}\n}\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u56de\u306f\u30a8\u30e9\u30fc\u51e6\u7406\u306e\u57fa\u672c\u3092\u78ba\u8a8d\u3057\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"author":1,"featured_media":968,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-4129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/4129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4129"}],"version-history":[{"count":8,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/4129\/revisions"}],"predecessor-version":[{"id":4152,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/4129\/revisions\/4152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/media\/968"}],"wp:attachment":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}