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

Map type alias assertion exception #59836

Closed
kaysonwu opened this issue Apr 26, 2023 · 2 comments
Closed

Map type alias assertion exception #59836

kaysonwu opened this issue Apr 26, 2023 · 2 comments

Comments

@kaysonwu
Copy link

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

$ go version
go version go1.20.3 darwin/arm64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/.../Library/Caches/go-build"
GOENV="/Users/.../Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/.../go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/.../go"
GOPRIVATE=""
GOPROXY="https://mirrors.aliyun.com/goproxy,direct"
GOROOT="/opt/homebrew/Cellar/go/1.20.3/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.20.3/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.3"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD=".../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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tn/_cb_j1ss0570vpbntrd714ww0000gn/T/go-build1602461495=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"reflect"
)

type M map[any]any

func main() {

	m := M{"products": M{"desk": M{"price": 100}}}

	var productsMap = reflect.ValueOf(m["products"])
	if productsMap.Kind() == reflect.Map {
		var deskMap = productsMap.MapIndex(reflect.ValueOf("desk"))
		if deskMap.Kind() == reflect.Map {
			var price = deskMap.MapIndex(reflect.ValueOf("price"))
			if price.Kind() == reflect.Int {
				fmt.Println(price.Int())
			}
		} else {
			fmt.Println("desk is not map")
		}
	}
}

// print:
// desk is not map

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

What did you expect to see?

desk Can be asserted as map[any]any and deskMap.Kind() is reflect.Map

What did you see instead?

desk Can't be asserted as map[any]any and deskMap.Kind() is reflect.Interface

keywords

assert, map, type alias

@go101
Copy link

go101 commented Apr 26, 2023

The kind of deskMap is Interface, not Map.

@seankhliao
Copy link
Member

working as intended.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2023
@golang golang locked and limited conversation to collaborators Apr 25, 2024
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