有人告诉我memCacheInstance有竞速问题,但go run -race说不出来。
代码:
type MemCache struct {
data []string
}
var memCacheInstance *MemCache
var memCacheCreateMutex sync.Mutex
func GetMemCache() *MemCache {
if memCacheInstance == nil {
memCacheCreateMutex.Lock()
defer memCacheCreateMutex.Unlock
就像这样想:
package first
type person struct {
Name string
}
var Per = person{
Name: "Jack",
}
在主包装中
package main
import "first"
import "fmt"
func main(){
o := first.Per
fmt.Println(o)
}
上面的工作原理是,我们可以看到第一个包中的变量在外部是可见的,但是它的类型不是,但它没有错误吗?它怎么能在外包装上有用?
作为背景,我有一个日志记录工具,它希望输出文件名和行号,以及管理其他每个调用者唯一的信息。在c++中,使用静态变量相对简单,但我很难在Go中找到相应的内容。
我遇到了runtime.Caller(),它将使我获得调用者的PC (从而唯一地识别它),但是以500‘s的速度计时--这不是我想在每次调用日志语句时都要支付的成本!
作为我行为的一个基本例子
package main
import (
"fmt"
"runtime"
)
type Info struct {
file string
lin
这个例子摘自“围棋之旅”:
显然,程序输出应该有10行:5行表示"hello“,5行表示"world”。
但我们有:
Linux -9行
MacOS - 10行
Linux输出(9行):
$ go run 1.go
hello
world
hello
world
hello
world
world
hello
hello
MacOS X输出(10行):
$ go run 1.go
hello
world
world
hello
hello
world
hello
world
hello
world
谁能解释一下-为什么
Linux uname -a
Linu
当我从golang.org参观Golang的时候,有一个代码片段我不明白:
func sum(a []int, c chan int, order int) {
sum := 0
for _, v := range a {
sum += v
}
fmt.Println(order, a)
c <- sum // 将和送入 c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
a1, a2 := a[:len(a)/2], a