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

for loop: breaks even if condition valuates to true #39933

Closed
amitbasuri opened this issue Jun 30, 2020 · 1 comment
Closed

for loop: breaks even if condition valuates to true #39933

amitbasuri opened this issue Jun 30, 2020 · 1 comment

Comments

@amitbasuri
Copy link

@amitbasuri amitbasuri commented Jun 30, 2020

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

$ go version
go version go1.13.6 darwin/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/amitbasuri/Library/Caches/go-build"
GOENV="/Users/amitbasuri/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=" -mod="
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/amitbasuri/goWork"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/amitbasuri/sdk-go/go1.13.6"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/amitbasuri/sdk-go/go1.13.6/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/amitbasuri/goWork/src/go-algo/go.mod"
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/9c/68n6djhd0yld73s97pfgyz0r0000gq/T/go-build587761073=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.13.6 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.13.6
uname -v: Darwin Kernel Version 17.7.0: Sun Dec  1 19:19:56 PST 2019; root:xnu-4570.71.63~1/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.13.6
BuildVersion:	17G10021
lldb --version: lldb-902.0.79.7
  Swift-4.1

What did you do?

I figured out something weird happening with the for loop, Please find my code below.
https://play.golang.org/p/gD8uCtJURYq

What did you expect to see?

The loop for root != nil || len(stack) > 0 { ...} should not exit if the value evaluates to true on the previous iteration.

What did you see instead?

The loop exits even if it evaluates to true.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 30, 2020

The statement

    root := stack[len(stack)-1]

shadows the root variable used by the loop condition. You may want to use = rather than := there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.