Skip to content

internal: slice initialization with colons should throw syntax error #70710

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
fumin opened this issue Dec 6, 2024 · 2 comments
Closed

internal: slice initialization with colons should throw syntax error #70710

fumin opened this issue Dec 6, 2024 · 2 comments

Comments

@fumin
Copy link

fumin commented Dec 6, 2024

Go version

go version go1.23.2 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/shaoyu/.cache/go-build'
GOENV='/home/shaoyu/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/shaoyu/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/shaoyu/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/shaoyu/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/shaoyu/a/qising/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3719528684=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I ran the following program, while intentionally mistyped a comma with a colon in slice initialization:

https://go.dev/play/p/uZWlwJV9cxI

package main

import "fmt"

func main() {
	// Correct syntax.
	a := []int{1, 3}

	// Wrong syntax, a bug!
	b := []int{1: 3}

	fmt.Println("a", a)
	fmt.Println("b", b)
}

What did you see happen?

The program prints:

a [1 3]
b [0 3]

No idea why the contents of b is as such.

What did you expect to see?

Since b is initialized not with a comma, the compiler should throw an syntax error.

@gabyhelp
Copy link

gabyhelp commented Dec 6, 2024

Related Issues

Related Discussions

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@fumin
Copy link
Author

fumin commented Dec 6, 2024

Duplicate of #30625 , closing.

@fumin fumin closed this as completed Dec 6, 2024
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

2 participants