Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to avoid data race #17

Open
xiaoxinmiao opened this issue Nov 25, 2018 · 0 comments
Open

to avoid data race #17

xiaoxinmiao opened this issue Nov 25, 2018 · 0 comments

Comments

@xiaoxinmiao
Copy link

xiaoxinmiao commented Nov 25, 2018

This code will cause data competition

package main

import (
	"fmt"
)

type Fruit struct {
	Name string
}


func main() {
	Race()
}
func Race() (newFruit Fruit) {
	go func() {
		newFruit = Fruit{Name: "Pear"}
	}()
	fmt.Printf("race3:exp:Pear,act:%v", newFruit.Name)
	return
}

//go run --race main.go
out put

PS D:\source\gopath\src\race\gofunc> go run --race main.go
race3:exp:Pear,act:==================
WARNING: DATA RACE
Write at 0x00c00003e1d0 by goroutine 6:
main.Race.func1()
	D:/source/gopath/src/race/gofunc/main.go:17 +0x3f

Previous read at 0x00c00003e1d0 by main goroutine:
runtime.convT2Estring()
	C:/GO/src/runtime/iface.go:332 +0x0
main.Race()
	D:/source/gopath/src/race/gofunc/main.go:19 +0x8e
main.main()
	D:/source/gopath/src/race/gofunc/main.go:13 +0x36

Goroutine 6 (running) created at:
main.Race()
	D:/source/gopath/src/race/gofunc/main.go:16 +0x6c
main.main()
	D:/source/gopath/src/race/gofunc/main.go:13 +0x36
==================
Found 1 data race(s)
exit status 66
xiaoxinmiao added a commit to xiaoxinmiao/echosample that referenced this issue Nov 25, 2018
xiaoxinmiao added a commit to xiaoxinmiao/echosample that referenced this issue Dec 5, 2018
xiaoxinmiao added a commit to xiaoxinmiao/echosample that referenced this issue Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant