go的interface接口使用案例

点击查看代码
package main

import (
	"fmt"
	"math/rand"
)

func testInterface() {
	var a interface {}  // interface可以存储任何类型的值
	fmt.Printf("00>> a=%#v %T\n",a,a)
	if a==nil{
		fmt.Println("01>> a is nil")
	}else{
		fmt.Println("01>> a is not nil")
	}
	var b int =100
	var c float32=3.2
	var d string="hello"
	a = b
	fmt.Printf("02>> a=%#v %T\n",a,a)
	fmt.Printf("02>> b=%#v %T\n",b,b)

	a =c
	fmt.Printf("03>> a=%#v %T\n",a,a)
	fmt.Printf("03>> c=%#v %T\n",c,c)

	a =d
	fmt.Printf("04>> a=%v %T\n",a,a)
	fmt.Printf("04>> d=%v %T\n",d,d)
}
 

func studentStore() {
	var stuMap map[int]map[string]interface {}
	stuMap = make(map[int]map[string]interface{},16)
	//插入学生信息 id=1,姓名=stu01,分数=78.5,年龄=14
	var id =1
	var name ="stu01"
	var score =78.2
	var age = 15
	value,ok := stuMap[id]
	if !ok{
		value = make(map[string]interface{},8)
	}
	value["name"] = name
	value["id"] = id
	value["score"] = score
	value["age"] = age
	stuMap[id] = value
	fmt.Printf("stumap:%#v\n",stuMap)
	
	for i:=0; i<10; i++{
		value,ok := stuMap[i]
		fmt.Printf("02 i= %d>> ok=%t value=%v \n",i, ok , value)

		if !ok{
			value = make(map[string]interface{},8)
		}
		value["name"] = fmt.Sprintf("stu%d",i)
		value["id"] = i
		value["score"] = rand.Float32()*100.0
		value["age"] = rand.Intn(100)
		stuMap[i] = value
	}
	fmt.Println()
	for k,v := range stuMap{
		fmt.Printf("id=%d stu_infoMAp: %#v\n",k,v)
	}
}

func main() {
	testInterface()
	//studentStore()
}

testInterface()输出:

点击查看代码
00>> a=<nil> <nil>
01>> a is nil
02>> a=100 int
02>> b=100 int
03>> a=3.2 float32
03>> c=3.2 float32
04>> a=hello string
04>> d=hello string

studentStore()输出:

点击查看代码
stumap:map[int]map[string]interface {}{1:map[string]interface {}{"age":15, "id":1, "name":"stu01", "score":78.2}}
02 i= 0>> ok=false value=map[]
02 i= 1>> ok=true value=map[age:15 id:1 name:stu01 score:78.2]
02 i= 2>> ok=false value=map[]
02 i= 3>> ok=false value=map[]
02 i= 4>> ok=false value=map[]
02 i= 5>> ok=false value=map[]
02 i= 6>> ok=false value=map[]
02 i= 7>> ok=false value=map[]
02 i= 8>> ok=false value=map[]
02 i= 9>> ok=false value=map[]

id=5 stu_infoMAp: map[string]interface {}{"age":11, "id":5, "name":"stu5", "score":51.521267}
id=8 stu_infoMAp: map[string]interface {}{"age":45, "id":8, "name":"stu8", "score":28.303415}
id=3 stu_infoMAp: map[string]interface {}{"age":40, "id":3, "name":"stu3", "score":6.563702}
id=6 stu_infoMAp: map[string]interface {}{"age":89, "id":6, "name":"stu6", "score":21.426388}
id=9 stu_infoMAp: map[string]interface {}{"age":6, "id":9, "name":"stu9", "score":67.90846}
id=1 stu_infoMAp: map[string]interface {}{"age":59, "id":1, "name":"stu1", "score":66.45601}
id=0 stu_infoMAp: map[string]interface {}{"age":87, "id":0, "name":"stu0", "score":60.466026}
id=2 stu_infoMAp: map[string]interface {}{"age":18, "id":2, "name":"stu2", "score":42.46375}
id=4 stu_infoMAp: map[string]interface {}{"age":0, "id":4, "name":"stu4", "score":9.696952}
id=7 stu_infoMAp: map[string]interface {}{"age":74, "id":7, "name":"stu7", "score":31.805817}

posted @ 2022-03-06 11:52  ty1539  阅读(44)  评论(0编辑  收藏  举报