GO 基础

package main

import (
	"fmt"
	"sync/atomic"
	"time"
)


func Chann(ch chan int,addr *int32,exitCh chan bool) {

	for {

		ret := atomic.LoadInt32(addr)
		if ret == 1 {

			break
		}
		ch <- 100
		time.Sleep(time.Second)
	}
	close(ch)
	println("线程安全退出")
	exitCh <- true
}

func timeOut(stopCh chan bool){
	time.Sleep(time.Second * 3)
	stopCh <- true
}

func test(count int)  {
	println("第",count,"次测试")
	ch := make(chan int)
	var n int32 = 0
	stopCh := make(chan bool)
	exitCh := make(chan bool)

	go Chann(ch,&n,exitCh)
	go timeOut(stopCh)

	for {
		select {
		case c,err := <-ch:
			if !err {
				fmt.Println("读管道出错 1",err)
			}else {
				fmt.Println("Recvice 1", c)
			}

		case s,err := <-ch:
			if !err {
				fmt.Println("读管道出错 2",err)
			}else {
				fmt.Println("Recvice 2", s)
			}
		case _ = <-stopCh:
			fmt.Println("超时")
			atomic.AddInt32(&n,1)
		case _ = <-exitCh:
			fmt.Println("安全退出")
			goto end
		}
	}
end:
}

type Phone struct {
	number string
}

func (phone *Phone) setNumber(num string)  {
	phone.number = num
}
//func (phone Phone) setNumber(num string)  {
//	phone.number = num
//}
func (phone *Phone) call() {
	fmt.Println("输出",phone.number)
}

type DeviceInfo struct {
	DeviceType string
	SystemNum string
}

type HuaweiPhone struct {
	Phone
	deviceinfo DeviceInfo
}

func NewHuaweiPhone() *HuaweiPhone {
	return &HuaweiPhone{deviceinfo: DeviceInfo{DeviceType: "1",SystemNum: "10.1"}}
}

type TextFun interface {
	SendText ()
}
func (phone HuaweiPhone) SendText() {
	fmt.Println("华为手机发送短信",phone.number)
}


func main() {
	var phone1 = new(Phone)
	var phone2 = new(Phone)
	phone1.setNumber("17712420000")
	phone2.setNumber("18812420000")
	phone1.call()
	phone2.call()

	huawei := NewHuaweiPhone()
	huawei2 := NewHuaweiPhone()
	huawei.deviceinfo.SystemNum = "10.01"
	huawei.number = "1234"
	huawei2.number = "456"
	huawei.call()
	huawei.SendText()
	huawei2.SendText()

	//for i := 1; i <= 10; i++ {
	//	test(i)
	//}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值