type DeptModeA interface {
Name() string
SetName(name string)
}
type DeptModeB interface {
Relocate(building string, floor uint8)
}
type Dept struct {
name string
building string
floor uint8
Key string
}
func (self Dept) Name() string {
return self.name
}
func (self Dept) SetName(name string) {
self.name = name
}
func (self *Dept) Relocate(building string, floor uint8) {
self.building = building
self.floor = floor
}
dept1 :=
Dept{
name: "MySohu",
building: "Internet",
floor: 7}
switch v := interface{}(dept1).(type) {
case DeptModeFull:
fmt.Printf("The dept1 is a DeptModeFull.\n")
case DeptModeB:
fmt.Printf("The dept1 is a DeptModeB.\n")
case DeptModeA:
fmt.Printf("The dept1 is a DeptModeA.\n")
default:
fmt.Printf("The type of dept1 is %v\n", v)
}
deptPtr1 := &dept1
if _, ok := interface{}(deptPtr1).(DeptModeFull); ok {
fmt.Printf("The deptPtr1 is a DeptModeFull.\n")
}
if _, ok := interface{}(deptPtr1).(DeptModeA); ok {
fmt.Printf("The deptPtr1 is a DeptModeA.\n")
}
if _, ok := interface{}(deptPtr1).(DeptModeB); ok {
fmt.Printf("The deptPtr1 is a DeptModeB.\n")
}
The dept1 is a DeptModeA. The deptPtr1 is a DeptModeFull. The deptPtr1 is a DeptModeA. The deptPtr1 is a DeptModeB.
package main
import (
"fmt"
)
type B struct {
Name string
}
func(b B) Test1() {
fmt.Printf("Test1 addr:%p\n", &b)
fmt.Printf("Test1 name:%s\n", b.Name)
b.Name = "john"
}
func(b *B) Test2() {
fmt.Printf("Test2 addr:%p\n", b)
fmt.Printf("Test2 name:%s\n", b.Name)
b.Name = "john"
}
func main() {
b := B{}
b.Test1()
b.Test1()
b.Test2()
b.Test2()
}
Test1 addr:0xc42000e1e0 Test1 name: Test1 addr:0xc42000e1f0 Test1 name: Test2 addr:0xc42000e1d0 Test2 name: Test2 addr:0xc42000e1d0 Test2 name:john
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有