《Go语言精进之路》1、2册书籍配套源代码。
本套书的勘误表可在这里查看。
购买套装书请点击这里。
Go语言精进之路书籍配套代码
License: Apache License 2.0
《Go语言精进之路》1、2册书籍配套源代码。
本套书的勘误表可在这里查看。
购买套装书请点击这里。
我觉得似乎 小"驼"峰拼写法 比 小"骆"峰拼写法更加常见。
return
是否更好理解呢?case <-quit:
分支完成后,在 main goroutine 没结束前 for 循环还会继续,worker 可能还会再工作func spawn(f func(int)) chan string {
quit := make(chan string)
go func() {
var job chan int // 模拟job channel
for {
select {
case j := <-job:
f(j)
case <-quit:
quit <- "ok"
+ return
}
}
}()
return quit
}
根据输出结果,性能最差的应该是range表达式的类型为切片的,而不是类型为数组的。
BenchmarkArrayRangeLoop-6 36454219 32.69 ns/op
BenchmarkPointerToArrayRangeLoop-6 34189981 33.13 ns/op
BenchmarkSliceRangeLoop-6 20230321 60.44 ns/op
I get a panic when I use tls.ID(), but I'm not sure how it works.
My version of Go?
go version go1.19.1 windows/amd64
Operating system?
win10
Here is my code, it comes from parallel_test.go
var (
m = make(map[int64]int, 20)
mu sync.Mutex
round int64 = 1
)
func BenchmarkParallel(b *testing.B) {
fmt.Printf("\ngoroutine[%d] enter BenchmarkParalell: round[%d], b.N[%d]\n",
tls.ID(), atomic.LoadInt64(&round), b.N)
defer func() {
atomic.AddInt64(&round, 1)
}()
b.RunParallel(func(pb *testing.PB) {
id := tls.ID()
fmt.Printf("goroutine[%d] enter loop func in BenchmarkParalell: round[%d], b.N[%d]\n", tls.ID(), atomic.LoadInt64(&round), b.N)
for pb.Next() {
mu.Lock()
_, ok := m[id]
if !ok {
m[id] = 1
} else {
m[id] = m[id] + 1
}
mu.Unlock()
}
mu.Lock()
count := m[id]
mu.Unlock()
fmt.Printf("goroutine[%d] exit loop func in BenchmarkParalell: round[%d], loop[%d]\n", tls.ID(), atomic.LoadInt64(&round), count)
})
fmt.Printf("goroutine[%d] exit BenchmarkParalell: round[%d], b.N[%d]\n",
tls.ID(), atomic.LoadInt64(&round), b.N)
}
Run this code with 'go test -bench . parallel_test.go -cpu=2', and it will occur panic:
goroutine[1] enter BenchmarkParalell: round[1], b.N[1]
goroutine[2] enter loop func in BenchmarkParalell: round[1], b.N[1]
goroutine[2] exit loop func in BenchmarkParalell: round[1], loop[1]
fatal error: goroutine[3] enter loop func in BenchmarkParalell: round[1], b.N[1]
faultgoroutine[3] exit loop func in BenchmarkParalell: round[1], loop[0]
fatal error: fault
[signal 0xc000001d code=0x0 addr=0x0 pc=0xdd53e1]
goroutine 69 [running]:
runtime.throw({0xe0a8ca?, 0xc0000c2240?})
C:/Program Files/Go/src/runtime/panic.go:1047 +0x65 fp=0xc000057f98 sp=0xc000057f68 pc=0xcf9865
runtime.sigpanic()
C:/Program Files/Go/src/runtime/signal_windows.go:273 +0x1af fp=0xc000057fe0 sp=0xc000057f98 pc=0xd0cc0f
runtime: g 69: unexpected return pc for github.com/huandu/go-tls.hackedGoexit called from 0x0
stack: frame={sp:0xc000057fe0, fp:0xc000057fe8} stack=[0xc000056000,0xc000058000)
0x000000c000057ee0: 0x0000000000000003 0x0000000000000002
0x000000c000057ef0: 0x0000000000000001 0x0000000000000000
0x000000c000057f00: 0x0000000000000001 0x000000c0000c2240
0x000000c000057f10: 0x0000000000de5120 0x0000000000d260d5 <runtime.systemstack+0x0000000000000035>
0x000000c000057f20: 0x0000000000cf9bac <runtime.fatalthrow+0x000000000000006c> 0x000000c000057f38
0x000000c000057f30: 0x000000c000084680 0x0000000000cf9be0 <runtime.fatalthrow.func1+0x0000000000000000>
0x000000c000057f40: 0x000000c000084680 0x0000000000cf9865 <runtime.throw+0x0000000000000065>
0x000000c000057f50: 0x000000c000057f68 0x000000c000057f88
0x000000c000057f60: 0x0000000000cf9865 <runtime.throw+0x0000000000000065> 0x000000c000057f70
0x000000c000057f70: 0x0000000000cf9880 <runtime.throw.func1+0x0000000000000000> 0x0000000000e0a8ca
0x000000c000057f80: 0x0000000000000005 0x000000c000057fd0
0x000000c000057f90: 0x0000000000d0cc0f <runtime.sigpanic+0x00000000000001af> 0x0000000000e0a8ca
0x000000c000057fa0: 0x000000c0000c2240 0x000000c000086098
0x000000c000057fb0: 0x000000c000084680 0x0000000000d802a0 <testing.(*B).RunParallel.func1.1+0x0000000000000000>
0x000000c000057fc0: 0x000000c0000860a0 0x000000c000057fb8
0x000000c000057fd0: 0x0000000000000000 0x0000000000dd53e1 <github.com/huandu/go-tls.hackedGoexit+0x0000000000000001>
0x000000c000057fe0: <0x0000000000000000 >0x0000000000000000
0x000000c000057ff0: 0x0000000000000000 0x0000000000000000
github.com/huandu/go-tls.hackedGoexit()
?:0 +0x1 fp=0xc000057fe8 sp=0xc000057fe0 pc=0xdd53e1
created by testing.(*B).RunParallel
C:/Program Files/Go/src/testing/benchmark.go:781 +0x105
goroutine 1 [chan receive]:
runtime.gopark(0x30000?, 0xc0000cb960?, 0xdf?, 0xd8?, 0x8?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc0000458e0 sp=0xc0000458c0 pc=0xcfc336
runtime.chanrecv(0xc000050180, 0x0, 0x1)
C:/Program Files/Go/src/runtime/chan.go:583 +0x49b fp=0xc000045970 sp=0xc0000458e0 pc=0xcc6b3b
runtime.chanrecv1(0xcd9846?, 0xf616e0?)
C:/Program Files/Go/src/runtime/chan.go:442 +0x18 fp=0xc000045998 sp=0xc000045970 pc=0xcc6678
testing.(*B).run1(0xc0000c2240)
C:/Program Files/Go/src/testing/benchmark.go:235 +0xb2 fp=0xc000045a58 sp=0xc000045998 pc=0xd7cf52
testing.(*B).Run(0xc0000c2000, {0xe0d8b3?, 0x142b94ac1e70?}, 0xe18390)
C:/Program Files/Go/src/testing/benchmark.go:676 +0x445 fp=0xc000045b40 sp=0xc000045a58 pc=0xd7f945
testing.runBenchmarks.func1(0xc0000c2000?)
C:/Program Files/Go/src/testing/benchmark.go:550 +0x6e fp=0xc000045b90 sp=0xc000045b40 pc=0xd7ebce
testing.(*B).runN(0xc0000c2000, 0x1)
C:/Program Files/Go/src/testing/benchmark.go:193 +0x102 fp=0xc000045c08 sp=0xc000045b90 pc=0xd7cd22
testing.runBenchmarks({0x0, 0x0}, 0xf60c40?, {0xf05740, 0x5, 0xcd741c?})
C:/Program Files/Go/src/testing/benchmark.go:559 +0x418 fp=0xc000045d08 sp=0xc000045c08 pc=0xd7e878
testing.(*M).Run(0xc00009c000)
C:/Program Files/Go/src/testing/testing.go:1733 +0x811 fp=0xc000045f00 sp=0xc000045d08 pc=0xd8a491
main.main()
_testmain.go:55 +0x1aa fp=0xc000045f80 sp=0xc000045f00 pc=0xdd616a
runtime.main()
C:/Program Files/Go/src/runtime/proc.go:250 +0x1fe fp=0xc000045fe0 sp=0xc000045f80 pc=0xcfbf9e
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc000045fe8 sp=0xc000045fe0 pc=0xd283e1
goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc000039fb0 sp=0xc000039f90 pc=0xcfc336
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:369
runtime.forcegchelper()
C:/Program Files/Go/src/runtime/proc.go:302 +0xb1 fp=0xc000039fe0 sp=0xc000039fb0 pc=0xcfc1d1
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc000039fe8 sp=0xc000039fe0 pc=0xd283e1
created by runtime.init.6
C:/Program Files/Go/src/runtime/proc.go:290 +0x25
goroutine 3 [GC sweep wait]:
runtime.gopark(0xf0ab01?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc00003bf90 sp=0xc00003bf70 pc=0xcfc336
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:369
runtime.bgsweep(0x0?)
C:/Program Files/Go/src/runtime/mgcsweep.go:297 +0xd7 fp=0xc00003bfc8 sp=0xc00003bf90 pc=0xce4937
runtime.gcenable.func1()
C:/Program Files/Go/src/runtime/mgc.go:178 +0x26 fp=0xc00003bfe0 sp=0xc00003bfc8 pc=0xcd9486
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00003bfe8 sp=0xc00003bfe0 pc=0xd283e1
created by runtime.gcenable
C:/Program Files/Go/src/runtime/mgc.go:178 +0x6b
goroutine 4 [GC scavenge wait]:
runtime.gopark(0x142cc2fa8a18?, 0xf355c?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc000047f70 sp=0xc000047f50 pc=0xcfc336
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:369
runtime.(*scavengerState).park(0xf0ad20)
C:/Program Files/Go/src/runtime/mgcscavenge.go:389 +0x53 fp=0xc000047fa0 sp=0xc000047f70 pc=0xce2973
runtime.bgscavenge(0x0?)
C:/Program Files/Go/src/runtime/mgcscavenge.go:622 +0x65 fp=0xc000047fc8 sp=0xc000047fa0 pc=0xce2f85
runtime.gcenable.func2()
C:/Program Files/Go/src/runtime/mgc.go:179 +0x26 fp=0xc000047fe0 sp=0xc000047fc8 pc=0xcd9426
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc000047fe8 sp=0xc000047fe0 pc=0xd283e1
created by runtime.gcenable
C:/Program Files/Go/src/runtime/mgc.go:179 +0xaa
goroutine 5 [finalizer wait]:
runtime.gopark(0x0?, 0xc00003de70?, 0xab?, 0x68?, 0xc00003df70?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc00003de28 sp=0xc00003de08 pc=0xcfc336
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:369
runtime.runfinq()
C:/Program Files/Go/src/runtime/mfinal.go:180 +0x10f fp=0xc00003dfe0 sp=0xc00003de28 pc=0xcd858f
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc00003dfe8 sp=0xc00003dfe0 pc=0xd283e1
created by runtime.createfing
C:/Program Files/Go/src/runtime/mfinal.go:157 +0x45
goroutine 18 [GC worker (idle)]:
runtime.gopark(0x142d02f2b168?, 0xd283e1?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc000049f50 sp=0xc000049f30 pc=0xcfc336
runtime.gcBgMarkWorker()
C:/Program Files/Go/src/runtime/mgc.go:1235 +0xf1 fp=0xc000049fe0 sp=0xc000049f50 pc=0xcdb691
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc000049fe8 sp=0xc000049fe0 pc=0xd283e1
created by runtime.gcBgMarkStartWorkers
C:/Program Files/Go/src/runtime/mgc.go:1159 +0x25
goroutine 7 [GC worker (idle)]:
runtime.gopark(0x142d02f2b168?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc000043f50 sp=0xc000043f30 pc=0xcfc336
runtime.gcBgMarkWorker()
C:/Program Files/Go/src/runtime/mgc.go:1235 +0xf1 fp=0xc000043fe0 sp=0xc000043f50 pc=0xcdb691
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc000043fe8 sp=0xc000043fe0 pc=0xd283e1
created by runtime.gcBgMarkStartWorkers
C:/Program Files/Go/src/runtime/mgc.go:1159 +0x25
goroutine 46 [runnable]:
runtime.gopark(0xc0000d5d70?, 0xcd68ab?, 0x20?, 0xc1?, 0xccd8df?)
C:/Program Files/Go/src/runtime/proc.go:363 +0xd6 fp=0xc0000d5d30 sp=0xc0000d5d10 pc=0xcfc336
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:369
runtime.semacquire1(0xc0000860a8, 0xf0?, 0x1, 0x0)
C:/Program Files/Go/src/runtime/sema.go:150 +0x20f fp=0xc0000d5d98 sp=0xc0000d5d30 pc=0xd0b64f
sync.runtime_Semacquire(0xc0001166f0?)
C:/Program Files/Go/src/runtime/sema.go:62 +0x25 fp=0xc0000d5dc8 sp=0xc0000d5d98 pc=0xd23b45
sync.(*WaitGroup).Wait(0x0?)
C:/Program Files/Go/src/sync/waitgroup.go:139 +0x52 fp=0xc0000d5df0 sp=0xc0000d5dc8 pc=0xd31e32
testing.(*B).RunParallel(0xc0000c2240, 0xc0000940f0)
C:/Program Files/Go/src/testing/benchmark.go:791 +0x1c9 fp=0xc0000d5e68 sp=0xc0000d5df0 pc=0xd80109
command-line-arguments.BenchmarkParallel(0xc0000c2240)
D:/Develop/tx/tx-go/gomaster/benchexamples/parallel_test.go:83 +0x171 fp=0xc0000d5f28 sp=0xc0000d5e68 pc=0xdd5a11
testing.(*B).runN(0xc0000c2240, 0x1)
C:/Program Files/Go/src/testing/benchmark.go:193 +0x102 fp=0xc0000d5fa0 sp=0xc0000d5f28 pc=0xd7cd22
testing.(*B).run1.func1()
C:/Program Files/Go/src/testing/benchmark.go:233 +0x59 fp=0xc0000d5fe0 sp=0xc0000d5fa0 pc=0xd7d2f9
fatal error: unexpected signal during runtime execution
panic during panic
[signal 0xc0000005 code=0x0 addr=0xe8 pc=0xd1c176]
runtime stack:
runtime.throw({0xe15231?, 0xee9ce0?})
C:/Program Files/Go/src/runtime/panic.go:1047 +0x65 fp=0xe00ebff570 sp=0xe00ebff540 pc=0xcf9865
runtime.sigpanic()
C:/Program Files/Go/src/runtime/signal_windows.go:249 +0x213 fp=0xe00ebff5b8 sp=0xe00ebff570 pc=0xd0cc73
runtime.gentraceback(0xe000000000?, 0x0?, 0xc00000cc00?, 0xe00ebff968?, 0x0, 0x0, 0x64, 0x0, 0x7ffff1404fd0?, 0x0)
C:/Program Files/Go/src/runtime/traceback.go:246 +0x8b6 fp=0xe00ebff930 sp=0xe00ebff5b8 pc=0xd1c176
runtime.traceback1(0xc0000376c0?, 0xe0a500?, 0x3?, 0xc0000376c0, 0x0?)
C:/Program Files/Go/src/runtime/traceback.go:846 +0x1b1 fp=0xe00ebffaf0 sp=0xe00ebff930 pc=0xd1e591
runtime.traceback(...)
C:/Program Files/Go/src/runtime/traceback.go:793
runtime.tracebackothers.func1(0xc0000376c0)
C:/Program Files/Go/src/runtime/traceback.go:1062 +0xe5 fp=0xe00ebffb30 sp=0xe00ebffaf0 pc=0xd1f4e5
runtime.forEachGRace(0xe00ebffb98)
C:/Program Files/Go/src/runtime/proc.go:592 +0x4d fp=0xe00ebffb60 sp=0xe00ebffb30 pc=0xcfcead
runtime.tracebackothers(0xc000084680?)
C:/Program Files/Go/src/runtime/traceback.go:1048 +0xe5 fp=0xe00ebffbc8 sp=0xe00ebffb60 pc=0xd1f3c5
runtime.dopanic_m(0xc000084680, 0xcf98d5?, 0xe0a8ca?)
C:/Program Files/Go/src/runtime/panic.go:1270 +0x225 fp=0xe00ebffc30 sp=0xe00ebffbc8 pc=0xcfa1c5
runtime.fatalthrow.func1()
C:/Program Files/Go/src/runtime/panic.go:1125 +0x48 fp=0xe00ebffc70 sp=0xe00ebffc30 pc=0xcf9c28
runtime.systemstack()
C:/Program Files/Go/src/runtime/asm_amd64.s:492 +0x4e fp=0xe00ebffc78 sp=0xe00ebffc70 pc=0xd260ee
[signal 0xc000001d code=0x0 addr=0x0 pc=0xdd53e1]
goroutine 68 [running]:
runtime.throw({0xe0a8ca?, 0xc0000c2240?})
C:/Program Files/Go/src/runtime/panic.go:1047 +0x65 fp=0xc000059f98 sp=0xc000059f68 pc=0xcf9865
runtime.sigpanic()
C:/Program Files/Go/src/runtime/signal_windows.go:273 +0x1af fp=0xc000059fe0 sp=0xc000059f98 pc=0xd0cc0f
runtime: g 68: unexpected return pc for github.com/huandu/go-tls.hackedGoexit called from 0xc0000211d0
stack: frame={sp:0xc000059fe0, fp:0xc000059fe8} stack=[0xc000058000,0xc00005a000)
0x000000c000059ee0: 0x0000000000d23bf9 <sync.runtime_Semrelease+0x0000000000000019> 0x0000000000000003
0x000000c000059ef0: 0x0000000000000000 0x0000000000000000
0x000000c000059f00: 0x000000c000059f38 0x0000000000d31d07 <sync.(*WaitGroup).Add+0x0000000000000087>
0x000000c000059f10: 0x0000000000de5120 0x0000000000d260d5 <runtime.systemstack+0x0000000000000035>
0x000000c000059f20: 0x0000000000cf9bac <runtime.fatalthrow+0x000000000000006c> 0x000000c000059f38
0x000000c000059f30: 0x000000c000037d40 0x0000000000cf9be0 <runtime.fatalthrow.func1+0x0000000000000000>
0x000000c000059f40: 0x000000c000037d40 0x0000000000cf9865 <runtime.throw+0x0000000000000065>
0x000000c000059f50: 0x000000c000059f68 0x000000c000059f88
0x000000c000059f60: 0x0000000000cf9865 <runtime.throw+0x0000000000000065> 0x000000c000059f70
0x000000c000059f70: 0x0000000000cf9880 <runtime.throw.func1+0x0000000000000000> 0x0000000000e0a8ca
0x000000c000059f80: 0x0000000000000005 0x000000c000059fd0
0x000000c000059f90: 0x0000000000d0cc0f <runtime.sigpanic+0x00000000000001af> 0x0000000000e0a8ca
0x000000c000059fa0: 0x000000c0000c2240 0x000000c000086098
0x000000c000059fb0: 0x000000c000037d40 0x0000000000d802a0 <testing.(*B).RunParallel.func1.1+0x0000000000000000>
0x000000c000059fc0: 0x000000c0000860a0 0x000000c000059fb8
0x000000c000059fd0: 0x0000000000000000 0x0000000000dd53e1 <github.com/huandu/go-tls.hackedGoexit+0x0000000000000001>
0x000000c000059fe0: <0x000000c0000211d0 >0x000000c000021200
0x000000c000059ff0: 0x000000c000021230 0x000000c000021260
github.com/huandu/go-tls.hackedGoexit()
?:0 +0x1 fp=0xc000059fe8 sp=0xc000059fe0 pc=0xdd53e1
created by testing.(*B).RunParallel
C:/Program Files/Go/src/testing/benchmark.go:781 +0x105
exit status 2
FAIL command-line-arguments 6.589s
FAIL
I found a similar issue: sirupsen/logrus#904, the final solution was using gis instead of tls.Btw, The correct spelling of "并行" is parallel.
第 1 行出现 float 类型,float 类型不存在,实际应为 float32 或者 float64,错误如下所示:
fnumbers := [...]float{}
// 应改为
fnumbers := [...]float64{}
微信读书
原书 Go 1.13版本在src下面增加了go.mod和go.num
go.num 应改为go.sum
我们看到, 在并发流水线启动预热并正式工作之后, 30名旅客的安检时长已经从3600下降到2160, 并发方案使得安检通道的效率有了进一步提升. 如果流水线持续工作, 效率应该会稳定在 1200(3600/3) 左右.
我对这里的 1200 有不同的看法.
如您书里面所说, 三个步骤耗时分别是
idCheckTmCost = 60
bodyCheckTmCost = 120
xRayCheckTmCost = 180
对于第三版安检线路, 由于每个模块都是并发执行, 因此这条线路的耗时取决于本线路中耗时最长的模块的总耗时.
这个耗时最高的模块就是xRayCheck模块
假设有1条这样的安检线路, 则耗时应该是: xRayCheckTmCost * 30 = 30 * 180 = 5400
假设有3条这样的安检线路, 则理想情况下的耗时应该是 xRayCheckTmCost * (30/3) = 180 * 10 = 1800
您这里简单的用 3600/3 = 1200 作为最终的最短耗时预期是不合理的
In chapter8/sources/benchmark-impl, the file use go-tls to get the go routine' s ID, but go-tls doesn't support go 1.17.*, maybe consider another way to get the go routine ID?
切片2线条应该从第三项开始,且cap=8
在Mac下面,进入到$GOROOT下的src目录中,执行以下命令:
cat $(find . -name '*.go') | indents | sort | uniq -c | sort -nr | sed 30q
无论是bash还是zsh,都会报错:command not found: indents
问题在:deferred_func_2.go
原文:
f, err := os.OpenFile(fname, os.O_RDWR, 0666)
if err != nil {
return err
}
defer f.Close()
改后:
f, err := os.OpenFile(fname, os.O_RDWR, 0666)
defer f.Close() // 这里
if err != nil {
return err
}
spinningthreads:处于自旋(spin)状态的操作系统数量。
应为
spinningthreads:处于自旋(spin)状态的操作系统线程的
数量。
版次: 2022年1月第一版第一次印刷
此页第一行
将未筛除的数的集合作为参数"传递归递给下去"。
应为typo
P66 最上面示例代码中 b.RunParallel(...) 没有与之匹配的右括号,将造成错误。
在微信读书的14.2中提到
如果你需要一个稳定的遍历次序,那么一个比较通用的做法是使用另一种数据结构来按需要的次序保存key,比如切片:
随后展示chapter3/sources/map_stable_iterate.go的示例代码:
身为读者读到这里误以为了切片能解决map在range中的乱序问题,并且示例代码的输出次序刚好跟map的原始初始次序是一致的
但!这仅仅是一个巧合而已。
作者在这里想要表达的应该是切片可以固定第一次range map的乱序,保证在后续的range中都是这样的乱序次序,而并不是说切片可以保持顺序、逆序或者map定义时的次序。
微信阅读上看的,页数不清楚,第七部分 第37条的小结
原文:
如果可以通过错误值类型的特征进行错误检视,那么尽量使用错误行为特征检视策略;在上述两种策略无法实施的情况下,再用“哨兵”策略和错误值类型检视策略;
感觉应该是:
如果可以通过错误值类型的特征进行错误检视,那么尽量使用错误值类型特征检视策略;在上述两种策略无法实施的情况下,再用“哨兵”策略和错误行为特征检视策略;
大神你好,在Airport Security Check的例子中(chapter6/source),下面语句似乎会被阻塞:
`func main() {
passengers := 30
queue := make(chan struct{}, 30)
newAirportSecurityCheckChannel("channel1", queue)
newAirportSecurityCheckChannel("channel2", queue)
newAirportSecurityCheckChannel("channel3", queue)
time.Sleep(5 * time.Second) // 保证上述三个goroutine都已经处于ready状态
for i := 0; i < passengers; i++ {
queue <- struct{}{}
}
time.Sleep(5 * time.Second)
close(queue) // 为了打印各通道的处理时长 <---- 这条语句会被阻塞
time.Sleep(1000 * time.Second)
}`
请问能否分析下,以及列举什么情况下关闭一个队列会被阻塞呢?谢谢。
原文:“因此,今天呈现在我们眼前的是这样的Go语言:...... 没有模板/泛型;”
Go现在有泛型了,这里的表述是不是应该改了?
最下面一段,私钥可以看“出“数对,应为看“成”。
method_set_9.go示例代码内容为女性员工:
// 返回女性员工总数
func FemaleCount(s Stmt) (int, error) {
result, err := s.Exec("select count(*) from employee_tab where gender=?", "1")
if err != nil {
return 0, err
}
return result.Int(), nil
}
但method_set_9_test.go的示例代码内容则为男性员工:
func TestEmployeeMaleCount(t *testing.T) {
f := fakeStmtForMaleCount{}
c, _ := MaleCount(f)
if c != 5 {
t.Errorf("want: %d, actual: %d", 5, c)
return
}
}
素数应当描述成“有且仅有两个截然不同的自然数因数,1和它本身”。原文缺少“仅有”两个,这使得任意一个正合数都可以满足这里的定义;且在中文语境中,“除数”显得也不太精确,建议使用因数代替。
代码的字体或许启用了连字,"fi"相互连接,两个字符合起来仅占用了一个字符的空间,并不是等宽的,阅读起来不够美观。这点是否可以通过关闭连字来解决?
type interface error { Error() string}
格式引文:
白明著.Go语言精进之路:从新手到高手的编程**、方法和技巧1.机械工业出版社华章分社.2022:642.
得到电子书:https://d.dedao.cn/DrTmNccjNtum4ouI
按照书中上下文与代码中的打印描述, 代码中的变量 u
应为数组,故初始化应为:u := [5]int{11, 12, 13, 14, 15}
或 u := [...]int{11, 12, 13, 14, 15}
。 书中为u := []int{11, 12, 13, 14, 15}
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.