Example #1
0
func interfaceTest2() {
	var s = new(interfacetest.S)
	var s2 interfacetest.S
	s2.Put(22)
	s.Put(100)
	fmt.Println("\n------------interface test---------\n")

	fmt.Println(g(s))
	fmt.Println(g(s2))
	fmt.Println(g(&s2))
	i := 5
	fmt.Println(g(i))
	fmt.Printf(" type is %T  %T   %T ", s, s2, i)
	//	g type is *interfacetest.S   g2 type is *interfacetest.S  &{100}  100
	//	g type is interfacetest.S   g2 type is <nil>  <nil>  -1
	//	g type is *interfacetest.S   g2 type is *interfacetest.S  &{22}  22
	//	g type is int   g2 type is <nil>  <nil>  -1
	//	type is *interfacetest.S  interfacetest.S   int
	fmt.Printf("\n------------interface test---------\n")
}
Example #2
0
func interfaceTest() {
	//var s = interfacetest.S(1)
	//s2 := interfacetest.S
	s := new(interfacetest.S)
	s2 := interfacetest.S(*s) //   interfacetest.S类型,不能作为interfacetest.InterTest的参数

	s.Age = 99
	s.Put(88)

	s2.Put(199)
	s2.Age = 999
	fmt.Printf("interfaceTest\n")

	interfacetest.InterTest(s)
	//接收者是指针不能传递值 正确
	//接受者是值能够传递指针 ?
	interfacetest.InterTest(&s2) //	interfacetest.S does not implement interfacetest.MyI (Get method requires pointer receiver)

	fmt.Printf("s type is  %T ,s2 type is %T\n", s, s2) //s type is  *interfacetest.S ,s2 type is interfacetest.S

	var mys interfacetest.S
	mys.Age = 88
	interfacetest.InterTest(&mys) //	interfacetest.S does not implement interfacetest.MyI (Get method requires pointer receiver)
	mys.Get()
	mys.Put(9)

	var mysw *interfacetest.S

	//mysw.Age = 90     //panic: runtime error: invalid memory address or nil pointer dereference
	fmt.Printf("mys type is  %T ,mysw type is %T value %v\n", mys, mysw, mysw) //mys type is  interfacetest.S ,mysw type is *interfacetest.S
	//mysw.Put(89)  //会出错  panic: runtime error: invalid memory address or nil pointer dereference
	mysw = s
	mysw.Age = 90
	mysw.Get()
	interfacetest.InterTest(mysw)

	var mys2 interfacetest.S2
	mys2.Age = 98
	interfacetest.InterTest(mys2)
	mys2.Get()

	fmt.Printf("mys type is  %T ,mys2 type is %T value %v\n", mys, mys2, mys2) //mys type is  interfacetest.S ,mys2 type is interfacetest.S2

	var mys2w *interfacetest.S2
	//mys2w.Age = 90 panic: runtime error: invalid memory address or nil pointer dereference
	//mys2w.Get() 会出错
	mys2w = &mys2
	mys2w.Age = 90
	interfacetest.InterTest(mys2w)
	mys2w.Get()
	fmt.Printf("mys2 type is  %T ,mys2w type is %T\n", mys2, mys2w) //mys2 type is  interfacetest.S2 ,mys2w type is *interfacetest.S2

	//t,ok := mys.(interfacetest.MyI)   //invalid type assertion: mys.(interfacetest.MyI) (non-interface type interfacetest.S on left)
	var myI interfacetest.MyI
	//myI.Put(199)    出错
	//var myI2 interfacetest.S2
	t, ok := myI.(interface{})

	//	if t, ok := mys.(interfacetest.MyI); ok {
	//		// 对于某些实现了接口I 的
	//		// t 是其所拥有的类型
	//	}

	fmt.Printf("myI type is  %T ,myI type is ok %v\n", t, ok) //myI type is  <nil> ,myI type is ok false

	t, ok = myI.(interfacetest.S2)
	fmt.Printf("myI type is  %T ,myI type is ok %v\n", t, ok) //myI type is  interfacetest.S2 ,myI type is ok false
	//a,b := s.(interfacetest.MyI)
	t, ok = myI.(interfacetest.MyI)
	fmt.Printf("myI type is  %T ,myI type is ok %v\n", t, ok) //myI type is  <nil> ,myI type is ok false

	var ddd interface{}
	ddd = s
	t, ok = ddd.(interfacetest.MyI)
	fmt.Printf("%T ddd type is  %T ,myI type is ok %v\n", ddd, t, ok) //*interfacetest.S ddd type is  *interfacetest.S ,myI type is ok true

	ddd = mys
	t, ok = ddd.(interfacetest.MyI)
	fmt.Printf("%T ddd type is  %T ,myI type is ok %v\n", ddd, t, ok) //interfacetest.S ddd type is  <nil> ,myI type is ok false

	var n *NameAge //在NameAge中的doSomething中用到了成员就会报错  anic: runtime error: invalid memory address or nil pointer dereference
	n = &NameAge{"name", 2}
	n.doSomething(2)

	var n2 NameAge
	n2.doSomething(2)
	fmt.Printf("\nn2 type is  %T , value is  %v\n", n2, n2)
	fmt.Printf("\nn type is  %T , value is  %v\n", n, n)

	var rtest *interfacetest.AA
	rtest.Get()
	fmt.Printf("\nrtest type is  %T , value is  %v\n", rtest, rtest) //rtest type is  *interfacetest.AA , value is  <nil>

	//下面有错误,上面没有错误
	//	var ftest *interfacetest.S
	//	ftest.Get()

}