From Go to documentation on method declarations :
The receiver type must be of the form T or * T, where T is the type name. T is called the base type of the receiver, or simply the base type. The base type must not be a pointer or interface type and must be declared in the same package as the method.
Can someone give me some idea of why this might be? Are there other (statically typed) languages that will allow this? I really want to define interface methods, so I can treat any instance of this type of interface as another. For example (steal an example from a Wikipedia article on a template method template ) if the following were true:
type Game interface { PlayOneGame(playersCount int) } type GameImplementation interface { InitializeGame() MakePlay(player int) EndOfGame() bool PrintWinner() } func (game *GameImplementation) PlayOneGame(playersCount int) { game.InitializeGame() for j := 0; !game.EndOfGame(); j = (j + 1) % playersCount { game.MakePlay(j) } game.PrintWinner() }
I could use any instance that implements "GameImplementation" as a "Game" without conversion:
var newGame Game newGame = NewMonopolyGame() // implements GameImplementation newGame.PlayOneGame(2)
: , . PlayBestOfThreeGames, , GameImplementation
, Java.
, , . Java , , , , , , Go - , .
, Go :
type GameImplementation interface { InitializeGame() MakePlay(player int) EndOfGame() bool PrintWinner() } func PlayOneGame(game GameImplementation, playersCount int) { game.InitializeGame() for j := 0; !game.EndOfGame(); j = (j + 1) % playersCount { game.MakePlay(j) } game.PrintWinner() }
PlayOneGame , , .
golang-nuts
, , : , . . Scala, Java, . Scala , Go.
, , , , , , .
Go - . , . , .
Go, , , ( ). , . . , , , , - , .
, Generics with Go, , , - . .
-, , - " ". , , , - . , , Java #, , , .
Go " ". , , . os.File.
os.File
( /, , , -, , , , .
- , , . , , , . , ( "" ), , .
, - , , , , , . - , Go , .
, , - - Go , -a-distance-y Go, .