Created
December 4, 2021 08:09
-
-
Save growvv/3340c880617f182d9d529d4c593374b3 to your computer and use it in GitHub Desktop.
copilot自动补全的go interface例子
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
package main | |
import "fmt" | |
type I interface { | |
M() | |
} | |
type T struct { | |
S string | |
} | |
func (t *T) M() { | |
if t == nil { | |
fmt.Println("<nil>") | |
return | |
} | |
fmt.Println(t.S) | |
} | |
type T2 struct { | |
S string | |
} | |
func (t *T2) M() { | |
if t == nil { | |
fmt.Println("<nil>") | |
return | |
} | |
fmt.Println(t.S) | |
} | |
func describe(i I) { | |
fmt.Printf("(%v, %T)\n", i, i) | |
} | |
func main() { | |
var i I | |
var t *T | |
i = t | |
describe(i) | |
i.M() | |
i = &T{"hello"} | |
describe(i) | |
i.M() | |
i = &T2{"hello"} | |
describe(i) | |
i.M() | |
m := make(map[string]interface{}) | |
m["hello"] = 1 | |
m["world"] = "hello" | |
m["t"] = T{"hello"} | |
m["t2"] = T2{"hello"} | |
fmt.Println(m) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment