Skip to content

Instantly share code, notes, and snippets.

@chenzhihao
Created October 14, 2019 04:36
Show Gist options
  • Save chenzhihao/51d2aa653a9ef712d8eab47b68624327 to your computer and use it in GitHub Desktop.
Save chenzhihao/51d2aa653a9ef712d8eab47b68624327 to your computer and use it in GitHub Desktop.
The concrete value stored in an interface is not addressable
package main
import (
"fmt"
)
type Orange struct {
}
/* this will not work, as the NewOrange return concrete as interface, which is not addressable
https://github.com/golang/go/wiki/MethodSets#interfaces
func (o *Orange) Name() string {
return "Orange"
}
*/
func (o Orange) Name() string {
return "Orange"
}
type Fruit interface {
Name() string
}
func NewOrange() Fruit {
return Orange{}
}
func main() {
orange := NewOrange()
fmt.Println(orange.Name())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment