Skip to content

Instantly share code, notes, and snippets.

@growvv
Created December 4, 2021 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save growvv/3340c880617f182d9d529d4c593374b3 to your computer and use it in GitHub Desktop.
Save growvv/3340c880617f182d9d529d4c593374b3 to your computer and use it in GitHub Desktop.
copilot自动补全的go interface例子
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