
Go
举个栗子.╭°
编程爱好者
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Go基础--29 不安全编程
unsafe经常用于与C程序交互package unsafeimport ( "testing" "unsafe")func TestUnsafe(t *testing.T){ i := 10 f := *(*float64)(unsafe.Pointer(&i)) t.Logf("%T",f)}type MyInt intfunc TestConvert(t *testing.T){ a := []int{1,2,3,4} b := *(*[]MyInt)(uns原创 2021-06-02 22:55:30 · 204 阅读 · 0 评论 -
Go基础--28 万能程序
package reflectimport ( "reflect" "testing")func TestDeepEqual(t *testing.T){ a := map[int]string{1:"one",2:"two",3:"three"} b := map[int]string{1:"one",2:"two",4:"three"} reflect.DeepEqual(a,b) c := []int{1,2,3} d := []int{22,33,44} reflect.D原创 2021-06-02 22:47:57 · 209 阅读 · 0 评论 -
Go基础 -- 27 反射编程
package reflectimport ( "fmt" "reflect" "testing")func CheckType(v interface{}){ t := reflect.TypeOf(v) switch t.Kind() { case reflect.Float32,reflect.Float64: fmt.Println("Float") case reflect.Int,reflect.Int64: fmt.Println("Int") defaul.原创 2021-06-02 22:29:03 · 185 阅读 · 0 评论 -
Go基础--26 Benchmark
package benchmarkimport ( "testing" "time")func BenchmarkByAdd(b *testing.B){ b.ResetTimer() for i:=1;i<10;i++{ time.Sleep(1 * time.Second) } b.StopTimer()}原创 2021-06-02 22:04:39 · 134 阅读 · 0 评论 -
Go基础--25 单元测试
package testingimport ( "fmt" "testing")func TestCalc(t *testing.T){ inputs := [...]int{1,2,3} expected := [...]int{1,2,9} for i:=0 ; i <len(inputs);i++{ ret := calc(inputs[i]) if ret != expected[i]{ t.Errorf("input is %d,the expected i原创 2021-06-02 21:58:56 · 92 阅读 · 0 评论 -
Go基础--24 sync.pool
sync.pool对象缓存尝试从私有对象获取私有对象不存在,尝试从当前Processor的共享池获取()如果当前Processpr共享池也是空,那么尝试去从其他Processor的共享池获取如果所有子池都是空,最后用户指定的New函数产生一个新的对象返回GC会清除sync.pool缓存对象对象的缓存有效期为下一次GC之前package poolimport ( "fmt" "runtime" "sync" "testing")func TestSyncPool(t *te原创 2021-06-02 21:46:21 · 96 阅读 · 0 评论 -
Go基础--23 对象池
数据库连接,网络连接通常会将数据库池化,避免重复创建package buffer_channelimport ( "errors" "fmt" "testing" "time")type ReusableObj struct {}type ObjPool struct { bufChan chan *ReusableObj}func NewObjPool(numOfObj int) *ObjPool{ ObjPool := ObjPool{} ObjPool.bufC原创 2021-06-02 21:37:42 · 257 阅读 · 0 评论 -
Go基础--22 只执行一次任务
package channel_closimport ( "fmt" "sync" "testing")func dataConsumer(ch chan int,wg *sync.WaitGroup){ go func() { for i:=0 ; i<10; i++{ if data,ok := <-ch;ok{ fmt.Println(data) }else{ fmt.Println("false") } } wg.Done(原创 2021-05-31 20:59:27 · 458 阅读 · 0 评论 -
Go基础--22 channel关闭和广播
channel通道关闭可以通过bool值来判断package channel_closimport ( "fmt" "sync" "testing")func dataConsumer(ch chan int,wg *sync.WaitGroup){ go func() { for i:=0 ; i<10; i++{ if data,ok := <-ch;ok{ fmt.Println(data) }else{ fmt.Println("fal原创 2021-05-29 10:00:58 · 170 阅读 · 0 评论 -
Go基础--21 多路选择和超时
select 多路复用和超时package _selectimport ( "fmt" "testing" "time")func service() string { time.Sleep(time.Millisecond * 500) return "Done"}func AsyncService() chan string{ retch := make(chan string) go func() { ret := service() fmt.Println("原创 2021-05-29 00:33:33 · 98 阅读 · 0 评论 -
Go基础--20 CSP并发机制
CSP vs Actor和Actor的直接通讯不同,CSP模式则是通过Channel进行通讯,更松耦合些Go中的channel是有容量限制并且独立于处理Groutine,而如Erlang,Actor模式中的mailbox容量是无限的,接收进程也总是被动的处理消息package cspimport ( "fmt" "testing" "time")func Service() string{ time.Sleep(time.Millisecond * 50) return "D原创 2021-05-28 07:53:19 · 107 阅读 · 0 评论 -
Go基础--19 共享内存机制
Lock 与 waitGrouppackage share_memoryimport ( "sync" "testing" "time")func TestCounter(t *testing.T){ counter := 0 for i:=0; i<5000; i++{ go func() { counter++ }() } time.Sleep( 1 * time.Second) t.Logf("counter = %d",counter)}func原创 2021-05-27 22:41:12 · 248 阅读 · 0 评论 -
Go基础--18 协程机制(未完)
Thread vs Groutine1.创建默认的stack的大小JDK5以后java Thread stack 默认是1MGroutine的stack初始化大小为2Kpackage goroutineimport ( "fmt" "testing" "time")func TestGroutine(t *testing.T) { for i:=0 ; i<10;i++{ go func(){ //此时i共享地址,造成竞态,需要加锁 fmt.Printl原创 2021-05-27 22:24:59 · 94 阅读 · 0 评论 -
Go基础--16 构建可复用的包
package基本复用模块单元(以首字母大写来表明可以被外部调用)代码的package可以和所在的目录不一致同一目录里的Go代码的package要保持一致package clientimport ( "go-learn/src/class16/series" "testing")func TestPackage(t *testing.T){ t.Log(series.GetFibonacci(10))}init方法在main函数执行之前,所有依赖的package的i原创 2021-05-27 21:50:44 · 118 阅读 · 0 评论 -
Go基础--15 pannic与recover
panicpannic用于不可恢复得错误panic退出前会执行deferpanic vs os.Exitos.Exit退出时不会调用defer指定函数os.Exit退出时不输出当前调用栈信息package panic_recoverimport ( "errors" "fmt" "testing")func TestPanicVsExit(t *testing.T){ fmt.Println("start") defer func() { fmt.Println(原创 2021-05-27 21:25:18 · 614 阅读 · 0 评论 -
Go基础--14 编写好的错误处理
及早判断错误package errorimport ( "errors" "testing")var LessThanTwoError = errors.New("n should be not less than 2")var LargeThanError = errors.New("n should be not more than 100")func GetFibonacci(n int) ([]int,error){ if n < 2 { return nil,Less原创 2021-05-27 21:13:48 · 158 阅读 · 0 评论 -
Go基础--13 不一样的接口类型 一样的多态
多态参数是interface 只能是指针类型package polymorphicimport ( "fmt" "testing")type code stringtype Programmer interface { WriteHelloWolrd() code}type GoProgrammer struct {}type JavaProgrammer struct {}func (p *GoProgrammer) WriteHelloWolrd()原创 2021-05-26 23:36:21 · 93 阅读 · 0 评论 -
Go基础--12 扩展与复用
Go不支持继承类似继承,但是无法实现重载机制package extensionimport ( "fmt" "testing")type Pet struct {}func (p *Pet) Speack(){ fmt.Println("miao")}func (p *Pet) SpeakTo(){ p.Speack() fmt.Println("...")}type Dog struct { Pet}func (d *Dog) Speack(){原创 2021-05-26 22:46:23 · 127 阅读 · 0 评论 -
Go基础--11 相关接口
Go接口实现是不依赖Go接口的定义--duck type 类似就可以package _interfaceimport "testing"type code stringtype Programmer interface { WirteHelloWolrd() code}type GoProgrammer struct {}func (g *GoProgrammer) WirteHelloWolrd() code{ return "fmt.Println(\"Hello.原创 2021-05-26 22:26:41 · 103 阅读 · 0 评论 -
Go基础--10 行为定义和实现
定义结构体package encapsulationimport "testing"type Employee struct { Id string Age int64 Name string}func TestCreateEmployee(t *testing.T){ e := Employee{"0",20,"leo"} e1 := Employee{Name:"ioe"} e2 := new(Employee) //指针类型 e2.Id = "20" e2.Name原创 2021-05-26 21:57:23 · 112 阅读 · 0 评论 -
Go基础--09 函数
函数是一等公民可以有多个返回值 所有参数传递都是值传递:slice map channel 会有传引用错觉 函数可以作为变量的值 函数可以作为参数和返回值package _funcimport ( "fmt" "math/rand" "testing" "time")func ReturnMultiValues()(int,int){ return rand.Intn(10),rand.Intn(10)}func TimeSpent(inner func(op int)原创 2021-05-26 21:37:19 · 89 阅读 · 0 评论 -
Go基础--08 字符串
字符串string是数据类型,不是引用或指针类型 string是只读的byte slice,len函数可以它所包含的byte数 string的byte数组可以存放任何数据原创 2021-05-26 21:15:12 · 75 阅读 · 0 评论 -
Go基础--07 map
map声明map声明后为空时候返回0package my_mapimport "testing"func TestInitMap(t *testing.T){ m1 := map[int]int{1:1,2:4,3:9} t.Log(m1[2]) t.Logf("len m1 %d",len(m1)) m2 := map[int]int{} m2[4] = 16 t.Logf("len m2=%d",len(m2)) m3 := make(map[int]int,10) //原创 2021-05-25 23:33:03 · 189 阅读 · 0 评论 -
Go基础--06 数组与切片
数组package arrayimport "testing"func TestArrayInit(t *testing.T){ var arr = [...]int{1,2,3} arr1 := [3]int{1,2} for i:=0;i<3;i++{ t.Log(arr[i]) } t.Log(arr1[1:])}func TestArrayLoop(t *testing.T){ arr3 := [...]int{1,2,3} for _,data := r原创 2021-05-25 23:07:16 · 126 阅读 · 1 评论 -
Go基础--05 循环与条件
循环Go语言仅支持循环关键字forpackage loopimport "testing"func TestWhileLoop(t *testing.T){ n := 0 for n < 5{ t.Log(n) n++ }}条件语句switch 不要加breakpackage conditionimport ( "runtime" "testing")func TestCondition(t *testing.T){ if a:=1 ==原创 2021-05-25 22:06:40 · 82 阅读 · 0 评论 -
Go基础-04 数组比较
数组比较package operator_testimport ( "testing")func TestCompareArray(t *testing.T){ a := [...]int{1,2,3} b := [...]int{1,2,3} c := [...]int{2,3,4} //相同位数才能比较 t.Log(a == b) t.Log(b == c)}原创 2021-05-25 21:41:38 · 600 阅读 · 0 评论 -
Go基础--03 类型
类型转化与其他编程语言差异Go语言不支持隐式类型转换 别名和原有类型也不能进行隐式类型转换package type_testimport "testing"type Myint intfunc TestImplict(t *testing.T){ var a int32 = 1 var b int64 b = int64(a) var c Myint a = int32(c) t.Log(a,b)}类型预定义的值math.MaxIn64 math.MaxFl原创 2021-05-25 21:33:56 · 80 阅读 · 0 评论 -
Go基础--02 变量
变量赋值赋值可以是进行自动类型推断 在一个赋值语句中可以对多个变量进行赋值斐波那契package fibimport ( "testing")func TestFibList(t *testing.T){ var ( a = 1 b = 1 ) t.Log(a) for i:=0 ; i < 5;i++{ t.Log(" ",b) tmp := a a = b b = tmp + a }}交换位置func TestExchang原创 2021-05-25 21:17:45 · 61 阅读 · 0 评论 -
Go基础--01 小试牛刀
Go语言必须以main函数作为入口main函数不支持传入参数,main函数不支持返回值package mainimport ( "fmt" "os")func main() { fmt.Println(os.Args) if len(os.Args) > 1{ fmt.Println("Hello wolrd",os.Args[1]) } fmt.Println("Hello Wolrd") os.Exit(1)}The master has failed原创 2021-05-23 22:58:47 · 70 阅读 · 0 评论