GO
文章平均质量分 83
ChaosMeta博客
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.”
展开
-
Golang逃逸与GC
一.Golang内存管理我们先来对比下C与Golang的内存分配: Golang内存分配特点:预先从操作系统申请一大块内存。 内存分配算法采用Google的 TCMalloc算法,预先将申请的内存分成不同大小的内存集合,给不同场景的内存使用。 回收内存会放入内存池,并不会直接分配给操作系统。介绍TCMalloc的几个重要概念Page:操作系统对内存管理以页...原创 2020-01-06 19:25:10 · 667 阅读 · 0 评论 -
Golang中Mutex的实现
一.Golang中的锁 Golang的提供的同步机制有sync模块下的Mutex、WaitGroup以及语言自身提供的chan等。 这些同步的方法都是以runtime中实现的底层同步机制(cas、atomic、spinlock、sem)为基础的。1 cas(Compare And Swap)和原子运算是其他同步机制的基础原子操作:指那些不能够被打断的操作被称为原子操作,当有...原创 2019-11-13 19:17:04 · 2326 阅读 · 0 评论 -
Golang-slice实现
一.slice实现1.1 slice结构体type slice struct { array unsafe.Pointer len int cap int}array:指向所引用的数组指针(unsafe.Pointer可以表示任何可寻址的值的指针) len:长度,当前引用切片的元素个数 cap:容量,当前引用切片的容量(底层数组的元素总数)在实际使用中,cap...原创 2019-10-23 19:52:43 · 197 阅读 · 0 评论 -
GO的学习路线
Step 1 —— 学习一门语言(Learn a Language)语言有很多选择,我已经将它们分成几类,以便更容易做出决定。对于刚开始进入后端开发的初学者,我建议你选择任何脚本语言,因为它们有很多功能,可以让你快速起步。如果你有一些前端知识,你可能会发现 Node.js 更容易(还有一个很大的就业市场)。如果你已经是后端开发并了解一些脚本语言,我建议不要选择其他脚本语言,而是从「函数式(...转载 2019-07-25 11:47:05 · 1676 阅读 · 0 评论 -
一.Go语言的特性
1.垃圾回收1.1内存自动回收,不用人为管理内存1.2只需new分配内存,不需要释放2.天然并发2.1从语言层面支持并发,非常简单2.2goroute,轻量级线程,创建成千上万个goroute成为可能2.3基于CSP(Communicating Sequential Process)模型实现3.channe3.1管道,类似于unix/linux中的pipe3...原创 2019-03-31 17:26:12 · 107 阅读 · 0 评论 -
二.GO语言的安装
一.链接 主页地址:https://golang.org/ 包地址:https://golang.org/pkg/ github地址:https://github.com/golang/go 下载地址:https://golang.org/dl/ 二.目录 $GOROOT 指向golang安装之后的根目录,windows平台下默认为...原创 2019-06-15 12:37:35 · 329 阅读 · 0 评论 -
三.GO语言的基本语法
一.基本数据类型和操作符1.1文件名、关键字、标识符1.1.1 命名规则:1.1.2 常用的关键字如下所示:关键字内置常量: true false iota nil内置类型: int int8 int16 int32 ...原创 2019-06-15 13:45:22 · 1509 阅读 · 1 评论 -
四.GO语言闭包和切片
一.闭包和匿名函数1.1 闭包的定义 函数可以嵌套定义(嵌套的函数一般为匿名函数),即在一个函数内部可以定义另一个函数。Go语言通过匿名函数支持闭包,C++不支持匿名函数,在C++11中通过Lambda表达式支持闭包。闭包是由函数及其相关引用环境组合而成的实体(即:闭包=函数+引用环境)。闭包只是在形式和表现上像函数,但实际上不是函数。函数是一些可执行的代码,函数代码在函数...原创 2019-06-15 15:15:30 · 250 阅读 · 0 评论 -
五.GO的并发模型
一 、什么是goroutine1.1 3种不同的线程用户级线程: 用户级线程是通过运行在用户态的运行时库来管理的,其优点是,线程的一切(包括调度、创建)都可以完全由用户自己决定,所以具有较高的灵活性。而且由于是在用户态上进行管理,所以就省去了内核管理的开销,所以具有高效率。 但是用户级线程有一个致命的缺点:一个进程内的某一个线程阻塞将导致整个进程内的所有线程全部阻塞。而...原创 2019-06-28 17:20:13 · 1919 阅读 · 0 评论 -
Golang对象池实现
一.sync.Pool定义 我们通常用golang来构建高并发场景下的应用,但是由于golang内建的GC机制会影响应用的性能,为了减少GC,golang提供了对象重用的机制,也就是sync.Pool对象池。 sync.Pool是可伸缩的,并发安全的。其大小仅受限于内存的大小,可以被看作是一个存放可重用对象的值的容器。 设计的目的是存放已经分配的但是暂时不用的对象,在需要用到的时候...原创 2019-08-22 18:52:23 · 2753 阅读 · 0 评论 -
Golang连接池
一.为什么需要Golang连接池 服务和服务之间的连接是开发过程中很常见的操作,为了服务解耦,减少相互依赖,增强系统稳定性,灵活性,所以会增加许许多多的服务通信链路,随着服务通信链路的增加,网络通信次数就会成倍的增长,那么随之而来的就是网络资源的消耗加剧,例如:带宽,连接数以及cpu,内存等。每个连接建立时都会申请内存用来做socket buffer 每个连接都要做三次握手四...原创 2019-08-19 20:48:41 · 2787 阅读 · 0 评论 -
Goroutine池实现
一.为什么要实现Goroutine池即便每个goroutine只分配4KB的内存,但如果是恐怖如斯的数量,聚少成多,内存会占用过高。 会对GC造成极大的负担,首先GC会在回收goroutine上消耗性能,其次GC本身也是goroutine,内存吃紧的状态下连GC的调度都会出现问题。 提高响应速度,减少创建协程的时间。 更好的管理协程,控制最大并发数量,定期回收。二.协程池的设计思路...原创 2019-08-19 18:47:52 · 986 阅读 · 0 评论 -
Golang-channel实现
一.简介 channel是Go语言在语言级别提供的goroutine间的通信方式,可以使用channel在两个或多个goroutine之间传递消息。channel是进程内通信方式,因此通过channel传递对象的过程和调用函数时的参数传递行为比较一致,比如也可以传递指针。如果需要跨进程通信,建议使用分布式系统来解决,比如使用Socket或者HTTP等通信协议。 ch...原创 2019-09-09 17:36:22 · 980 阅读 · 0 评论