我需要读取从另一个 goroutine 设置的结构字段,即使知道肯定不会有并发访问(在读取发生之前写入完成,通过 发出信号chan struct{})也可能会导致陈旧数据
考虑到我可以保证没有并发访问,发送指向结构的指针(在第一个 goroutine 中创建,在第二个 goroutine 中修改,在第三个 goroutine 中读取)会解决可能的陈旧问题吗?
我想避免复制,因为结构很大并且包含填充在第二个 goroutine 中的巨大 Bytes.Buffer,我需要从第三个 goroutine 中读取
有一个锁定选项,但考虑到我知道不会有并发访问,这似乎有点矫枉过正
Update: The question title can be misleading. This was not Go's fault at all.
See the first comment or the accepted answer.
Run Code Online (Sandbox Code Playgroud)
以下代码(好吧,几乎相同)计算Linux下的页面视图,但在Windows下计算它们是双倍的.
有人可以找出原因吗?
package main
import (
"fmt"
"http"
)
func main() {
println("Running")
http.HandleFunc("/", makeHomeHandler())
http.ListenAndServe(":8080", nil)
}
// this version compiles and run OK under the Linux version of Golang
func makeHomeHandler() func(c *http.Conn, r *http.Request) {
views := 1
return func(c *http.Conn, r *http.Request) {
fmt.Fprintf(c, "Counting %s, %d so far.", r.URL.Path[1:], views)
views++
}
} …Run Code Online (Sandbox Code Playgroud)