Go:不安全编程

By kcersing , 11 四月, 2020

import (
    "fmt"
    "sync"
    "sync/atomic"
    "testing"
    "time"
    "unsafe"
)
type Customer struct {
    Name string
    Age int
}
//不安全编程
func TestUnsafe(t *testing.T){
    i:=10
    f:=*(*float64)(unsafe.Pointer(&i))
    t.Log(unsafe.Pointer(&i))
    t.Log("====================")
    t.Log(f)
}
type MyInt int
func TestConvert(t *testing.T)  {
    a:=[]int{1,2,3,4}
    b:=*(*[]MyInt)(unsafe.Pointer(&a))
    t.Log(b)
}
//原子类型操作
func TestAtomic(t *testing.T)  {
    var shareBufPtr unsafe.Pointer
    writeDataFn:= func() {
        data:=[]int{}
        for i:=0 ;i<100;i++  {
            data = append(data, i)
        }
        atomic.StorePointer(&shareBufPtr,unsafe.Pointer(&data))
    }
    readDataFn:= func() {
        data:=atomic.LoadPointer(&shareBufPtr)
        fmt.Println(data,*(*[]int)(data))
    }
    var wg sync.WaitGroup
    writeDataFn()
    for i:=0;i<100 ;i++  {
        wg.Add(1)
        go func(){
            for i:=0;i<10 ;i++  {
                writeDataFn()
                time.Sleep(time.Millisecond*100)
            }
            wg.Done()
            }()
        wg.Add(1)
        go func () {
            for i:=0;i<10 ;i++  {
                readDataFn()
                time.Sleep(time.Millisecond*100)
            }
            wg.Done()
        }()
    }
}


标签