Skip to content

go bug sync.Map #32641

Closed
Closed
@Enroban

Description

@Enroban

What version of Go are you using (go version)?

$ go version
go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/enroban/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/enroban/go:/Users/enroban/Documents/任务/poker/Programming/poker-server"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z8/7vhfw24x0wj_2vs5nlvmwz2h0000gn/T/go-build163069748=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

When I wrote my code, I used sync. Map, a multi-threaded, secure Map officially provided by Go, because of the need for multi-threading. Because of the need, I use a two-dimensional sync. Map, that is, sync. Map will contain another pile of sync. Map, at this time, very strange things happened, sync. Map I saved was empty when it was taken out? Unless I use a normal map to store this pile of sync. Maps, it's empty to take them out.

func main() {
	var a =  make(map[string]sync.Map)
	var b sync.Map
	b.Store("zifu","zifu")
	a["b"]=b
	fmt.Println("a",a)
	fmt.Println("b",b)
	c:=a["b"]
	fmt.Println("c",c)
	d,ok:=c.Load("zifu")
	if ok {
		fmt.Println("d",d)
	}

	var e sync.Map
	fmt.Println("e",e)
	b.Store("e",e)
	fmt.Println("a",a)
	fmt.Println("b",b)
	f,ok := b.Load("e")
	//下面的代码会有运行时错误
	//f,ok := a["b"].Load("e")
	if ok {
		fmt.Println("f",f)
	}
}

What did you expect to see?

a map[b:{{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}]
b {{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}
c {{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}
d zifu
a map[b:{{0 0} {{map[] true}} map[e:0xc00000c038 zifu:0xc00000c028] 0}]
b {{0 0} {{map[] true}} map[zifu:0xc00000c028 e:0xc00000c038] 0}
f{{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}

What did you see instead?

a map[b:{{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}]
b {{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}
c {{0 0} {{map[] true}} map[zifu:0xc00000c028] 0}
d zifu
a map[b:{{0 0} {{map[] true}} map[e:0xc00000c038 zifu:0xc00000c028] 0}]
b {{0 0} {{map[] true}} map[zifu:0xc00000c028 e:0xc00000c038] 0}
f {{0 0} {<nil>} map[] 0}

OR

# command-line-arguments
./main.go:28:16: cannot call pointer method on a["b"]
./main.go:28:16: cannot take the address of a["b"]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions