Created
October 14, 2019 04:36
-
-
Save chenzhihao/51d2aa653a9ef712d8eab47b68624327 to your computer and use it in GitHub Desktop.
The concrete value stored in an interface is not addressable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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