tiancaiamao / go-internals Goto Github PK
View Code? Open in Web Editor NEWdig into implemention of the go programming language
dig into implemention of the go programming language
package main
import (
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
var count int
var ch = make(chan bool, 1)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
ch <- true
count++
time.Sleep(time.Millisecond)
count--
<-ch
wg.Done()
}()
}
wg.Wait()
}
这段代码文章中说执行go run -race test.go 会检测到数据竞争, 我本地go1.7.3执行并没有检测到竞态。
2.2 slice
一个slice是一个数组某个部分的引用。在内存中,它是一个3字长结构
slice 由一个指针 一个len 一个cap 在AMD64下仅仅占3个字长么?
https://tiancaiamao.gitbooks.io/go-internals/content/zh/02.4.html
error和Error这个是故意的嘛?
其中 <内存模型相关bug一例> 一节给出的有bug例子,在现在的版本中已经有保证了。
The kth receive on a channel with capacity C happens before the k+Cth send from that channel completes.
感觉有点奇怪, 是否内容丢失, 或者把目录的链接去掉更好?
https://github.com/tiancaiamao/go-internals/blob/master/ebook/05.0.md
关于channel的happens-before在Go的内存模型中提到了三种情况:
1.对一个channel的发送操作 happens-before 相应channel的接收操作完成
2.关闭一个channel happens-before 从该Channel接收到最后的返回值0
3.不带缓冲的channel的接收操作 happens-before 相应channel的发送操作完成
为什么带缓冲的 发送操作 happens-before 相应channel的接收操作完成??
不带缓冲的channel的接收操作 happens-before 相应channel的发送操作完成??
发现如果调用c函数,然后该函数异常,这个时候如果处理了异常信号,直接return。则会产生该函数不停重复调用的现象。
runtime.cgocall里面干了什么呢?
在这个例子中,如果buffered chan 的长度为1,是不会有bug的吧?我在本地使用go run -race运行也不会有任何报错,您是否把buffered chan的长度误写为1?如果不是,麻烦您能详细解释一下吗,谢谢。
看w3cschool过来的,特来膜拜大神~
fmt.Print("大佬牛逼")
我只是好奇go的interface是如何实现的/是一个胖指针?还是类似c++在对象中对应每个父类放置一个vptr?blah,blah.似乎在目录中根本找不到只写方面的内容.书中go对象模型内容缺失?
这可改如何处理啊
参照 https://github.com/tiancaiamao/go-internals/blob/master/zh/05.5.md 内容, 我想验证一下文中的抢占调度,写了以下代码,发现和预期的不太一致,请教一下是什么问题。
预期如下:
但是print并没有机会执行,是否说明 loop 中调用 delta()时,并没有检查 stackguard 状态.
package main
import (
"fmt"
"runtime"
"time"
)
//go:noinline
func delta() int {
return 1
}
func loop() {
var a int
for {
// 调用一个函数, 预期会触发检查 stackguard 状态, 从而被停止运行, 让print有机会执行
a += delta()
}
}
func print() {
fmt.Print("prompted")
}
func main() {
var forever chan int
runtime.GOMAXPROCS(1)
// 保证loop先于print执行
go loop()
time.Sleep(time.Second)
go print()
<-forever
}
您好,我差点没能找到这个项目,还要能搜索到我也在写开源书 https://golang.coding3min.com,一起交流
slice的扩容规则三步走
计算预估容量大小 : 如果预估扩容后的slice的容量大于原来的容量的2倍,则预估容量为扩容后的大小。
例如:
arr := []int{1,2}
arr = append(arr, []int{3,4,5}...)
计算扩容后需要占用的内存大小:元素大小有关,在64位机器下int为8字节
8 * 5 = 40 // 需要40字节
计算最终容量大小:
go采用的是基于tcmalloc进行的内存分配,也就是go语言自己实现的内存分配器。
其内存分配规则如下
// 内存 划分
var class_to_size = [_NumSizeClasses]uint16{0, 8, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 288, 320, 352, 384, 416, 448, 480, 512, 576, 640, 704, 768, 896, 1024, 1152, 1280, 1408, 1536, 1792, 2048, 2304, 2688, 3072, 3200, 3456, 4096, 4864, 5376, 6144, 6528, 6784, 6912, 8192, 9472, 9728, 10240, 10880, 12288, 13568, 14336, 16384, 18432, 19072, 20480, 21760, 24576, 27264, 28672, 32768}
//runtime下sizeclasses.go文件
扩容时若步骤2需要40字节大小的slice,那么内存分配器不会给40字节,而是选择大等于40字节,并最接近的大小的48字节。
即
48 / 8(int 64位下占用的大小) = 6
则扩容后的容量为 6
您好,非常感谢您的无私奉献,我学习到一些东西。我想请问下,好久没更新内容,是不准备更新了吗?
Go源码安装章节 指向Mercurial的链接404.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.