Skip to content

Abnormal number increment #46098

Closed
Closed
@storyicon

Description

@storyicon

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

$ go version
go version go1.16.3 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\storyicon\AppData\Local\go-build
set GOENV=C:\Users\storyicon\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\Go
set GOPRIVATE=
set GOROOT=D:\Program Files\golang
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=D:\Program Files\golang\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\STORYI~1\AppData\Local\Temp\go-build2449719716=/tmp/go-build -gno-record-gcc-switches

What did you do?

image

package main

import (
	"fmt"
	"time"
)

func main(){
	i := 0
	go func() {
		for {
			i++
		}
	}()
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for {
		select {
		case <-ticker.C:
			fmt.Printf("i=%d \r\n", i)
		}
	}
}

What did you expect to see?

i should be increasing

What did you see instead?

i is always equal to 0

This seems like a very simple problem, but it doesn't meet intuitive expectations.

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