Skip to content

error for using len() function to control a channel #22435

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

Closed
Yang-YiFan opened this issue Oct 25, 2017 · 2 comments
Closed

error for using len() function to control a channel #22435

Yang-YiFan opened this issue Oct 25, 2017 · 2 comments

Comments

@Yang-YiFan
Copy link

Yang-YiFan commented Oct 25, 2017

Please answer these questions before submitting your issue. Thanks!

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

go1.9.1 linux/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/yangyf/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build419888304=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

just a very simple Go program

package main
import "fmt"
import "time"

func main() {
	a:=make(chan int,10)
	go func() {
		for {
			if(len(a)!=0) {
				x:=<-a
				fmt.Println(x)
				if x==-1 {
					break
				}
			}
		}
	}()

	time.Sleep(2*time.Second)
	a<-1
	a<-2
	a<-3
	time.Sleep(2*time.Second)
	a<-6
	a<-7
	a<- -1
        time.Sleep(5*time.Second)
}

What did you expect to see?

the program should behave as if the line if (len(a))!=0 doesn't exist
that is channel a receive the data sent from main function and print it
it will print 1,2,3,6,7,-1 respectively

What did you see instead?

instead there is no print at all
if I print len(a), it will constantly show len(a)=0 rather than some positive value
in some sense channel a is not working? I don't know why
so the program can't stop

@ianlancetaylor
Copy link
Contributor

Closing as dup of #10958.

It's unfortunate, and we're working on it. But also, don't write programs this way. This program will burn an entire CPU core doing nothing.

@heschi
Copy link
Contributor

heschi commented Oct 25, 2017

(If you got a notification for a previous comment from me, please ignore, I meant to put that on #22434.)

@golang golang locked and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants