{"id":495,"date":"2018-10-26T16:21:58","date_gmt":"2018-10-26T07:21:58","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/10\/26\/go-duck-typing"},"modified":"2022-10-26T07:20:16","modified_gmt":"2022-10-25T22:20:16","slug":"go-duck-typing","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/10\/26\/go-duck-typing\/","title":{"rendered":"Go\u8a00\u8a9e\u3067\u306e\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0"},"content":{"rendered":"

\"\"<\/p>\n

\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u5185\u5c71\u3067\u3059\u3002
\n\u3077\u3088\u3077\u3088e\u30b9\u30dd\u30fc\u30c4\u304c500\u5186\u30bb\u30fc\u30eb\u3092\u3084\u3063\u3066\u3044\u305f\u306e\u3067\u3001\u7121\u99c4\u306bSwitch\u7248\u3068PS4\u7248\u3092\u8cb7\u3044\u307e\u3057\u305f\u3002
\n\u4e45\u3057\u3076\u308a\u306b\u3077\u3088\u3077\u3088\u3092\u697d\u3057\u3093\u3067\u3044\u307e\u3059\u3002<\/p>\n

\u4eca\u56de\u306f\u3001\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u3068\u306f<\/h2>\n

\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u3068\u306f\u3001Ruby\u306a\u3069\u306e\u52d5\u7684\u578b\u4ed8\u3051\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u884c\u308f\u308c\u308b\u3001\u578b\u4ed8\u3051\u306e\u3084\u308a\u65b9\u3067\u3059\u3002<\/p>\n

\u6b21\u306e\u6587\u7ae0\u304c\u57fa\u306b\u306a\u3063\u3066\u3044\u308b\u8003\u3048\u65b9\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

\n

\u3082\u3057\u3082\u305d\u308c\u304c\u30a2\u30d2\u30eb\u306e\u3088\u3046\u306b\u6b69\u304d\u3001\u30a2\u30d2\u30eb\u306e\u3088\u3046\u306b\u9cf4\u304f\u306e\u306a\u3089\u3001\u305d\u308c\u306f\u30a2\u30d2\u30eb\u3067\u3042\u308b\u3002<\/p>\n

\u30c0\u30c3\u30af\u30fb\u30bf\u30a4\u30d4\u30f3\u30b0 - Wikipedia<\/a><\/p>\n<\/blockquote>\n

\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306e\u8a71\u306b\u7f6e\u304d\u63db\u3048\u308b\u3068\u3001
\u305d\u308c<\/code>\u304c\u300c\u4f55\u304b\u3057\u3089\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u300d
\u30a2\u30d2\u30eb\u306e\u3088\u3046\u306b\u6b69\u304d<\/code> \u30a2\u30d2\u30eb\u306e\u3088\u3046\u306b\u9cf4\u304f<\/code> \u304c\u300c\u30e1\u30bd\u30c3\u30c9\u300d
\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u5148\u307b\u3069\u306e\u6587\u7ae0\u3092\u8a00\u3044\u63db\u3048\u308b\u3068\u3001<\/p>\n

\n

\u3082\u3057\u3082\u305d\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c Duck\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u306e\u3088\u3046\u306b Walk()\u30e1\u30bd\u30c3\u30c9 \u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u3001Duck\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u3088\u3046\u306b Sound()\u30e1\u30bd\u30c3\u30c9 \u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u306a\u3089\u3001\u305d\u308c\u306f Duck\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u3068\u3057\u3066\u6271\u3048\u308b\u3002<\/p>\n<\/blockquote>\n

\u3068\u306a\u308b\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n

Ruby\u3067\u306e\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0<\/h2>\n

Ruby\u3067\u306f\u3001\u30e1\u30bd\u30c3\u30c9\u3067\u53d7\u3051\u53d6\u308b\u5f15\u6570\u306e\u578b\u3092\u5236\u9650\u3057\u307e\u305b\u3093\u3002<\/p>\n

\u4ee5\u4e0b\u306e test \u30e1\u30bd\u30c3\u30c9\u3067\u306f\u3001\u5f15\u6570 foo \u306e\u578b\u3092\u5236\u9650\u3057\u3066\u3044\u307e\u305b\u3093\u3002foo \u304b\u3089 sound \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308c\u3070\u554f\u984c\u306f\u8d77\u3053\u308a\u307e\u305b\u3093\u3002<\/p>\n

def test(foo)\n  puts foo.sound\nend<\/code><\/pre>\n

\u3053\u306e\u3088\u3046\u306b\u3001\u300c\u5f15\u6570 foo \u304c\u3069\u306e\u3088\u3046\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3042\u308c\u3001 sound \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u305b\u308c\u3070\u826f\u3044\u300d\u3068\u5272\u308a\u5207\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u3001\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u3067\u3059\u3002<\/strong><\/p>\n

\u5bfe\u3057\u3066\u3001\u4ee5\u4e0b\u306eJava\u30b3\u30fc\u30c9\u3067\u306f\u3001\u53d7\u3051\u53d6\u308b\u5f15\u6570 foo \u306e\u578b\u3092 Duck \u306b\u5236\u9650\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

void test(Duck foo) {\n   foo.sound();\n}<\/code><\/pre>\n

\u3053\u306e\u5834\u5408\u3001test \u30e1\u30bd\u30c3\u30c9\u306f\u3001Duck \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u4f9d\u5b58\u3057\u3066\u304a\u308a\u3001\u7d50\u5408\u5ea6\u304c\u9ad8\u3044\u72b6\u614b\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

Java\u3067interface\u3092\u4f7f\u3063\u305f\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0(\u306e\u3088\u3046\u306a\u3082\u306e)<\/h2>\n

Java\u3067\u3082interface\u3092\u4f7f\u3063\u3066\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u3068\u540c\u3058\u3088\u3046\u306a\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

interface ISomething {\n  void sound();\n}\n\nvoid test(ISomething foo) {\n  foo.sound();\n}<\/code><\/pre>\n

test \u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306b\u306f Something \u3092\u5b9f\u88c5(sound\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5)\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3042\u308c\u3070\u3001\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u578b Duck \u3092\u53d7\u3051\u53d6\u308b\u5f62\u3088\u308a\u7d50\u5408\u5ea6\u306f\u4f4e\u3044\u3067\u3059\u306d\u3002<\/p>\n

\u4f8b\u3048\u3070\u3001test\u30e1\u30bd\u30c3\u30c9\u306bDuckMock\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n

class DuckMock implements ISomething {\n  void sound() {\n    system.out.println("Ga-Ga-Mock-");\n  }\n}<\/code><\/pre>\n

DuckMock \u306f\u3001ISomething \u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3092__\u660e\u793a\u7684\u306b\u5ba3\u8a00\u3057\u3066\u3044\u307e\u3059__\u3002<\/p>\n

Go\u8a00\u8a9e\u306e\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0<\/h2>\n

Go\u8a00\u8a9e\u306b\u3082interface\u304c\u3042\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3051\u307e\u3059\u3002<\/p>\n

type Something interface {\n  Sound()\n}\n\n\/\/ Something \u3092\u53d7\u3051\u53d6\u308b\u95a2\u6570\nfunc test(foo Something) {\n    foo.Sound()\n}<\/code><\/pre>\n

Java\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306e\u5b9f\u88c5\u3068\u7570\u306a\u308b\u70b9\u306f\u3001__implements \u306a\u3069\u3067\u660e\u793a\u7684\u306b\u5ba3\u8a00\u3057\u306a\u304f\u3066\u3082\u826f\u3044\u3068\u3053\u308d\u3067\u3059\u3002__DuckMock \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\u3002<\/p>\n

type DuckMock struct {}\n\nfunc (d *DuckMock) Sound() {\n    fmt.Println("Ga-Ga-")\n}<\/code><\/pre>\n

\u5358\u7d14\u306b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3068\u540c\u3058\u30b7\u30b0\u30cb\u30c1\u30e3\u3092\u6301\u3063\u305f\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3060\u3051\u3067\u6e08\u307f\u307e\u3059\u3002<\/p>\n

\u3053\u306e\u70b9\u306f\u3001\u3088\u308a\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u306b\u8fd1\u3044\u5f62\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u8a00\u3048\u307e\u3059\u3002Go\u8a00\u8a9e\u306e interface \u306e\u6a5f\u80fd\u306f__\u578b\u4ed8\u304d\u306e\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0__\u3068\u8a00\u3048\u307e\u3059\u3002<\/p>\n

\u30e1\u30bd\u30c3\u30c9\u304c\u306a\u3044interface\u306f\u3069\u3046\u3044\u3063\u305f\u578b\u3092\u53d7\u3051\u53d6\u308c\u308b\u304b\uff1f<\/h2>\n

\u4ee5\u4e0b\u306eGo\u8a00\u8a9e\u306e\u30b3\u30fc\u30c9\u3067\u3001foo \u306f\u3069\u3046\u3044\u3063\u305f\u578b\u3092\u53d7\u3051\u53d6\u308b\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n

func test(foo interface{}) {\n   ...\n}<\/code><\/pre>\n

__\u7b54\u3048\u306f\u300c\u3069\u3093\u306a\u578b\u3067\u3082\u53d7\u3051\u53d6\u308b\u300d\u3067\u3059\u3002__\u4e00\u3064\u3082\u30e1\u30bd\u30c3\u30c9\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u3069\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3082\u5f53\u3066\u306f\u307e\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<\/p>\n

Go\u8a00\u8a9e\u3067\u306f\u3001\u8272\u3005\u306a\u578b\u3092\u53d7\u3051\u53d6\u308a\u305f\u3044\u5834\u5408\u306f\u3001\u3053\u308c\u3092\u5fdc\u7528\u3057\u307e\u3059\u3002\u6975\u529b\u907f\u3051\u305f\u307b\u3046\u304c\u826f\u3044\u3067\u3059\u304c\u3002<\/p>\n

\u307e\u3068\u3081<\/h2>\n

Go\u8a00\u8a9e\u3067\u306e\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u307f\u307e\u3057\u305f\u3002
\n\u4ed6\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3092\u3084\u3063\u3066\u304d\u305f\u4eba\u304c\u5c11\u3057\u6238\u60d1\u3046\u30dd\u30a4\u30f3\u30c8\u3060\u3068\u601d\u3046\u306e\u3067\u3001\u7406\u89e3\u306e\u52a9\u3051\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u5185\u5c71\u3067\u3059\u3002 \u3077\u3088\u3077\u3088e\u30b9\u30dd\u30fc\u30c4\u304c500\u5186\u30bb\u30fc\u30eb\u3092\u3084\u3063\u3066\u3044\u305f\u306e\u3067\u3001\u7121\u99c4\u306bSwitch\u7248\u3068PS4\u7248\u3092\u8cb7\u3044\u307e\u3057\u305f\u3002 \u4e45\u3057\u3076\u308a\u306b\u3077\u3088\u3077\u3088\u3092\u697d\u3057\u3093\u3067\u3044\u307e\u3059\u3002 \u4eca\u56de\u306f\u3001\u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u3068\u306f \u30c0\u30c3\u30af\u30bf\u30a4\u30d4\u30f3\u30b0\u3068\u306f\u3001Ruby\u306a\u3069\u306e\u52d5\u7684\u578b\u4ed8\u3051\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u884c\u308f\u308c\u308b\u3001\u578b\u4ed8\u3051\u306e\u3084\u308a\u65b9\u3067\u3059\u3002 \u6b21\u306e\u6587\u7ae0\u304c\u57fa\u306b\u306a\u3063\u3066\u3044\u308b\u8003\u3048\u65b9\u3068\u306a\u3063\u3066\u3044\u307e\u3059 […]<\/p>\n","protected":false},"author":5,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[128],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/495"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":3302,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/495\/revisions\/3302"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}