-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: support more type assertion cases
Fixes #955
- Loading branch information
Showing
3 changed files
with
213 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type MyWriter interface { | ||
Write(p []byte) (i int, err error) | ||
} | ||
|
||
type TestStruct struct{} | ||
|
||
func (t TestStruct) Write(p []byte) (n int, err error) { | ||
return len(p), nil | ||
} | ||
|
||
func usesWriter(w MyWriter) { | ||
w.Write(nil) | ||
} | ||
|
||
type MyStringer interface { | ||
String() string | ||
} | ||
|
||
func usesStringer(s MyStringer) { | ||
fmt.Println(s.String()) | ||
} | ||
|
||
func main() { | ||
var t interface{} | ||
t = TestStruct{} | ||
var tw MyWriter | ||
var ok bool | ||
tw, ok = t.(MyWriter) | ||
if !ok { | ||
fmt.Println("TestStruct does not implement MyWriter") | ||
} else { | ||
fmt.Println("TestStruct implements MyWriter") | ||
usesWriter(tw) | ||
} | ||
|
||
var tt interface{} | ||
tt = time.Nanosecond | ||
var myD MyStringer | ||
myD, ok = tt.(MyStringer) | ||
if !ok { | ||
fmt.Println("time.Nanosecond does not implement MyStringer") | ||
} else { | ||
fmt.Println("time.Nanosecond implements MyStringer") | ||
usesStringer(myD) | ||
} | ||
} | ||
|
||
// Output: | ||
// TestStruct implements MyWriter | ||
// time.Nanosecond implements MyStringer | ||
// 1ns |
This file contains 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
This file contains 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