{"id":15060514,"url":"https://github.com/rhcarvalho/assert-interface","last_synced_at":"2026-01-02T09:08:25.600Z","repository":{"id":66869409,"uuid":"68244585","full_name":"rhcarvalho/assert-interface","owner":"rhcarvalho","description":"Comparison of different ways to assert a type implements an interface in Go","archived":false,"fork":false,"pushed_at":"2016-09-14T21:24:56.000Z","size":5,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-11T18:41:06.819Z","etag":null,"topics":["assembly","go","golang","interfaces"],"latest_commit_sha":null,"homepage":null,"language":"Assembly","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rhcarvalho.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-14T21:24:39.000Z","updated_at":"2024-06-19T13:19:59.425Z","dependencies_parsed_at":"2023-05-31T16:01:21.471Z","dependency_job_id":null,"html_url":"https://github.com/rhcarvalho/assert-interface","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhcarvalho%2Fassert-interface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhcarvalho%2Fassert-interface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhcarvalho%2Fassert-interface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhcarvalho%2Fassert-interface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rhcarvalho","download_url":"https://codeload.github.com/rhcarvalho/assert-interface/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243685657,"owners_count":20330996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembly","go","golang","interfaces"],"created_at":"2024-09-24T22:59:43.259Z","updated_at":"2026-01-02T09:08:25.573Z","avatar_url":"https://github.com/rhcarvalho.png","language":"Assembly","readme":"# How do I assert that a type implements an interface in Go?\n\nThis is a simple demo to compare different ways of asserting that a type\nimplements an interface.\n\nOne way of doing it is following a pattern that can be found in the [Go standard\nlibrary](https://github.com/golang/go):\n\n```go\nvar _ Interface = (*Implementation)(nil)\n```\n\nAn alternative pattern, found in projects such as\n[Kubernetes](https://github.com/kubernetes/kubernetes) and [OpenShift\nOrigin](https://github.com/openshift/origin) is:\n\n```go\nvar _ = Interface(\u0026Implementation{})\n```\n\nAre those two forms equality good?  \n**Yes!** They produce the same assembly code.\n\nTested with Go 1.4, 1.6 and 1.7.\n\n\n## Trivia\n\n### 1. Error messages\n\nIn case the type does not implement the interface, the error messages from the\ncompiler are slightly different:\n\n```console\n$ go build ./example\n# github.com/rhcarvalho/assert-interface/example\nexample/stdlib.go:8: cannot use (*HelperImplementation)(nil) (type *HelperImplementation) as type helper.Interface in assignment:\n        *HelperImplementation does not implement helper.Interface (missing Help method)\n```\n\n```console\n$ go build -tags kube ./example\n# github.com/rhcarvalho/assert-interface/example\nexample/kube.go:8: cannot convert HelperImplementation literal (type *HelperImplementation) to type helper.Interface:\n        *HelperImplementation does not implement helper.Interface (missing Help method)\n```\n\n### 2. The blank identifier (`_`)\n\nThe [_blank identifier_](https://golang.org/ref/spec#Blank_identifier) serves as\nan anonymous placeholder instead of a regular (non-blank) identifier and has\nspecial meaning in declarations: it does not introduce a binding and thus is not\ndeclared.\n\nWhen using a regular variable name instead of `_` in `stdlib.go` and `kube.go`,\nthe two patterns produce different assemblies, the second pattern causing an\nextra allocation.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhcarvalho%2Fassert-interface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frhcarvalho%2Fassert-interface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhcarvalho%2Fassert-interface/lists"}