1
1
<!--{
2
2
"Title": "The Go Programming Language Specification",
3
- "Subtitle": "Version of July 31 , 2019",
3
+ "Subtitle": "Version of Aug 26 , 2019",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -1244,16 +1244,15 @@ <h3 id="Interface_types">Interface types</h3>
1244
1244
</ p >
1245
1245
1246
1246
< pre class ="ebnf ">
1247
- InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
1248
- MethodSpec = MethodName Signature | InterfaceTypeName .
1247
+ InterfaceType = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" .
1248
+ MethodSpec = MethodName Signature .
1249
1249
MethodName = identifier .
1250
1250
InterfaceTypeName = TypeName .
1251
1251
</ pre >
1252
1252
1253
1253
< p >
1254
- As with all method sets, in an interface type, each method must have a
1255
- < a href ="#Uniqueness_of_identifiers "> unique</ a >
1256
- non-< a href ="#Blank_identifier "> blank</ a > name.
1254
+ An interface type may specify methods < i > explicitly</ i > through method specifications,
1255
+ or it may < i > embed</ i > methods of other interfaces through interface type names.
1257
1256
</ p >
1258
1257
1259
1258
< pre >
@@ -1265,6 +1264,11 @@ <h3 id="Interface_types">Interface types</h3>
1265
1264
}
1266
1265
</ pre >
1267
1266
1267
+ < p >
1268
+ The name of each explicitly specified method must be < a href ="#Uniqueness_of_identifiers "> unique</ a >
1269
+ and not < a href ="#Blank_identifier "> blank</ a > .
1270
+ </ p >
1271
+
1268
1272
< pre >
1269
1273
interface {
1270
1274
String() string
@@ -1280,9 +1284,9 @@ <h3 id="Interface_types">Interface types</h3>
1280
1284
</ p >
1281
1285
1282
1286
< pre >
1283
- func (p T) Read(p []byte) (n int, err error) { return … }
1284
- func (p T) Write(p []byte) (n int, err error) { return … }
1285
- func (p T) Close() error { return … }
1287
+ func (p T) Read(p []byte) (n int, err error)
1288
+ func (p T) Write(p []byte) (n int, err error)
1289
+ func (p T) Close() error
1286
1290
</ pre >
1287
1291
1288
1292
< p >
@@ -1332,27 +1336,41 @@ <h3 id="Interface_types">Interface types</h3>
1332
1336
< p >
1333
1337
An interface < code > T</ code > may use a (possibly qualified) interface type
1334
1338
name < code > E</ code > in place of a method specification. This is called
1335
- < i > embedding</ i > interface < code > E</ code > in < code > T</ code > ; it adds
1336
- all (exported and non-exported) methods of < code > E</ code > to the interface
1337
- < code > T</ code > .
1339
+ < i > embedding</ i > interface < code > E</ code > in < code > T</ code > .
1340
+ The < a href ="#Method_sets "> method set</ a > of < code > T</ code > is the < i > union</ i >
1341
+ of the method sets of < code > T</ code > ’s explicitly declared methods and of
1342
+ < code > T</ code > ’s embedded interfaces.
1338
1343
</ p >
1339
1344
1340
1345
< pre >
1341
- type ReadWriter interface {
1342
- Read(b Buffer) bool
1343
- Write(b Buffer) bool
1346
+ type Reader interface {
1347
+ Read(p []byte) (n int, err error)
1348
+ Close() error
1344
1349
}
1345
1350
1346
- type File interface {
1347
- ReadWriter // same as adding the methods of ReadWriter
1348
- Locker // same as adding the methods of Locker
1349
- Close()
1351
+ type Writer interface {
1352
+ Write(p []byte) (n int, err error)
1353
+ Close() error
1350
1354
}
1351
1355
1352
- type LockedFile interface {
1353
- Locker
1354
- File // illegal: Lock, Unlock not unique
1355
- Lock() // illegal: Lock not unique
1356
+ // ReadWriter's methods are Read, Write, and Close.
1357
+ type ReadWriter interface {
1358
+ Reader // includes methods of Reader in ReadWriter's method set
1359
+ Writer // includes methods of Writer in ReadWriter's method set
1360
+ }
1361
+ </ pre >
1362
+
1363
+ < p >
1364
+ A < i > union</ i > of method sets contains the (exported and non-exported)
1365
+ methods of each method set exactly once, and methods with the
1366
+ < a href ="#Uniqueness_of_identifiers "> same</ a > names must
1367
+ have < a href ="#Type_identity "> identical</ a > signatures.
1368
+ </ p >
1369
+
1370
+ < pre >
1371
+ type ReadCloser interface {
1372
+ Reader // includes methods of Reader in ReadCloser's method set
1373
+ Close() // illegal: signatures of Reader.Close and Close are different
1356
1374
}
1357
1375
</ pre >
1358
1376
0 commit comments