{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/13534","labels_url":"https://api.github.com/repos/golang/go/issues/13534/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/13534/comments","events_url":"https://api.github.com/repos/golang/go/issues/13534/events","html_url":"https://github.com/golang/go/issues/13534","id":121009828,"number":13534,"title":"Returning pointer to struct allows returns for structs that don't implement the required interface type in method signature","user":{"login":"stuarthicks","id":868156,"avatar_url":"https://avatars.githubusercontent.com/u/868156?v=3","gravatar_id":"","url":"https://api.github.com/users/stuarthicks","html_url":"https://github.com/stuarthicks","followers_url":"https://api.github.com/users/stuarthicks/followers","following_url":"https://api.github.com/users/stuarthicks/following{/other_user}","gists_url":"https://api.github.com/users/stuarthicks/gists{/gist_id}","starred_url":"https://api.github.com/users/stuarthicks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stuarthicks/subscriptions","organizations_url":"https://api.github.com/users/stuarthicks/orgs","repos_url":"https://api.github.com/users/stuarthicks/repos","events_url":"https://api.github.com/users/stuarthicks/events{/privacy}","received_events_url":"https://api.github.com/users/stuarthicks/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-08T13:48:20Z","updated_at":"2015-12-08T14:21:56Z","closed_at":"2015-12-08T14:21:56Z","body":"I've believe I've uncovered what is, at best, confusing behaviour in the way structs are associated with interfaces, and at worst, a bug that can hide other issues.\r\n\r\nTake the following example go program:\r\n\r\n```go\r\npackage main\r\n\r\nimport \"github.com/davecgh/go-spew/spew\"\r\n\r\ntype BarInterface interface {\r\n\tBar() string\r\n}\r\n\r\ntype Foo struct{}\r\n\r\n// Incorrect pointer receiver, doesn't actually implement BarInterface correctly.\r\nfunc (f *Foo) Bar() string {\r\n\treturn \"bar\"\r\n}\r\n\r\nfunc foo() BarInterface {\r\n\tf := &Foo{}\r\n\t// I'm returning a *Foo instead of Foo, and method signature says return is BarInterface (and Foo doesn't implement BarInterface)\r\n\treturn f\r\n}\r\n\r\nfunc main() {\r\n\tf := foo() // Returns BarInterface\r\n\tspew.Dump(f) // (*main.Foo)\r\n}\r\n```\r\n\r\nI don't expect this to compile because I'm returning a pointer to a struct, instead of a value in the return of `foo()`. In any case, this works and spew prints out correctly that `f` is `*main.Foo`.\r\n\r\nHowever if I were to try do a type assertion (such as `t, ok := f.(BarInterface)` I get a compile error because of the pointer receiver on the implementation of `Bar()`. From this it seems that there's actually two issues with the return call in `Foo()`:\r\n\r\n- It's returning a pointer instead of a value, which doesn't match the method signature.\r\n- The struct doesn't actually correctly implement the interface and should error on that return line (and in-fact does error if I dereference `f` in the return).\r\n\r\nWould appreciate an explanation if this is intended behaviour (even better if it's documented somewhere I haven't found yet). Otherwise I believe this to be a bug.\r\n\r\n> What version of Go are you using (go version)?\r\n\r\nI've tried this on 1.5.1 and 1.5.2 (same result).\r\n\r\n> What operating system and processor architecture are you using?\r\n\r\nOSX El Capitan. x86_64.\r\n\r\n> What did you do?\r\n\r\nReturned a pointer to a struct instead of the value. Struct doesn't correctly implement the interface that's declared as the return type for that method.\r\n\r\nWhat did you expect to see?\r\n\r\nCompile errors.\r\n\r\nWhat did you see instead?\r\n\r\nNo compile errors, and a return value that doesn't match the method signature."},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/162894801","html_url":"https://github.com/golang/go/issues/13534#issuecomment-162894801","issue_url":"https://api.github.com/repos/golang/go/issues/13534","id":162894801,"user":{"login":"ianlancetaylor","id":3194333,"avatar_url":"https://avatars.githubusercontent.com/u/3194333?v=3","gravatar_id":"","url":"https://api.github.com/users/ianlancetaylor","html_url":"https://github.com/ianlancetaylor","followers_url":"https://api.github.com/users/ianlancetaylor/followers","following_url":"https://api.github.com/users/ianlancetaylor/following{/other_user}","gists_url":"https://api.github.com/users/ianlancetaylor/gists{/gist_id}","starred_url":"https://api.github.com/users/ianlancetaylor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianlancetaylor/subscriptions","organizations_url":"https://api.github.com/users/ianlancetaylor/orgs","repos_url":"https://api.github.com/users/ianlancetaylor/repos","events_url":"https://api.github.com/users/ianlancetaylor/events{/privacy}","received_events_url":"https://api.github.com/users/ianlancetaylor/received_events","type":"User","site_admin":false},"created_at":"2015-12-08T14:21:56Z","updated_at":"2015-12-08T14:21:56Z","body":"You haven't shown any bug here.  We use the issue tracker only for bugs; we prefer that people ask questions as described at https://golang.org/wiki/Questions .  Thanks.\r\n\r\nYou have some misunderstanding about types, methods, and interfaces, but I'm not sure precisely what it is.  `BarInterface` is satisfied by any type that implements the method `Bar() string`.  The type `*Foo` implements that interface.  The function `foo` in your example returns a value of type `*Foo`.  That type implements the interface, so all is well.  I'm not sure why you think it wouldn't be."}}