Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
aef4e1c3
Commit
aef4e1c3
authored
Mar 04, 2011
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc: add "implements" question to FAQ
R=r, fw, gri, r2, yiyus CC=golang-dev
https://golang.org/cl/4248051
parent
ee1cb829
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
0 deletions
+65
-0
doc/go_faq.html
doc/go_faq.html
+65
-0
No files found.
doc/go_faq.html
View file @
aef4e1c3
...
@@ -508,6 +508,71 @@ Regarding operator overloading, it seems more a convenience than an absolute
...
@@ -508,6 +508,71 @@ Regarding operator overloading, it seems more a convenience than an absolute
requirement. Again, things are simpler without it.
requirement. Again, things are simpler without it.
</p>
</p>
<h3
id=
"implements_interface"
>
Why doesn't Go have "implements" declarations?
</h3>
<p>
A Go type satisfies an interface by implementing the methods of that interface,
nothing more. This property allows interfaces to be defined and used without
having to modify existing code. It enables a kind of "duck typing" that
promotes separation of concerns and improves code re-use, and makes it easier
to build on patterns that emerge as the code develops.
The semantics of interfaces is one of the main reasons for Go's nimble,
lightweight feel.
</p>
<p>
See the
<a
href=
"#inheritance"
>
question on type inheritance
</a>
for more detail.
</p>
<h3
id=
"guarantee_satisfies_interface"
>
How can I guarantee my type satisfies an interface?
</h3>
<p>
You can ask the compiler to check that the type
<code>
T
</code>
implements the
interface
<code>
I
</code>
by attempting an assignment:
</p>
<pre>
type T struct{}
var _ I = T{}
</pre>
<p>
If
<code>
T
</code>
doesn't implement
<code>
I
</code>
, the mistake will be caught
at compile time.
</p>
<p>
If you wish the users of an interface to explicitly declare that they implement
it, you can add a method with a descriptive name to the interface's method set.
For example:
</p>
<pre>
type Fooer interface {
Foo()
ImplementsFooer()
}
</pre>
<p>
A type must then implement the
<code>
ImplementsFooer
</code>
method to be a
<code>
Fooer
</code>
, clearly documenting the fact.
</p>
<pre>
type Bar struct{}
func (b Bar) ImplementsFooer() {}
func (b Bar) Foo() {}
</pre>
<p>
Most code doesn't make use of such constraints, since they limit the utility of
the interface idea. Sometimes, though, they're necessary to resolve ambiguities
among similar interfaces.
</p>
<h2
id=
"values"
>
Values
</h2>
<h2
id=
"values"
>
Values
</h2>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment