Skip to content

template: golang can not get template type #70487

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
epowsal opened this issue Nov 21, 2024 · 3 comments
Closed

template: golang can not get template type #70487

epowsal opened this issue Nov 21, 2024 · 3 comments

Comments

@epowsal
Copy link

epowsal commented Nov 21, 2024

Go version

1.23 windows amd64

Output of go env in your module/workspace:

set GO111MODULE=off
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\iwlb\AppData\Local\go-build
set GOENV=C:\Users\iwlb\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=true
set GOMODCACHE=M:\work\code\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=M:\work\code\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn
set GOROOT=M:\work\tool\go1.23.1.windows-amd64
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=M:\work\tool\go1.23.1.windows-amd64\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.1
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\iwlb\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\iwlb\AppData\Local\Temp\go-build4257316669=/tmp/go-build -gno-record-gcc-switches

What did you do?

try get type but code can not compile.

func (tree *Tree[K, V]) MaxMatch(key K) *Iterator[K, V] {
	node := tree.Root
	for node != nil {
		var compare int
		switch key.(type) {//compile error!switch K will get error too.
		case string:
			nkl := len(node.Key.(string))
			if kl >= len(key.(string)) {
				compare = strings.Compare(node.Key.(string), key.(string)[:nkl])
			} else {
				return &Iterator[K, V]{tree: tree, node: nil, position: end}
			}
		default:
			compare = tree.Comparator(key, node.Key)
		}
		switch {
		case compare == 0:
			return &Iterator[K, V]{tree: tree, node: node, position: between, maxmakey: key}
		case compare < 0:
			node = node.Left
		case compare > 0:
			node = node.Right
		}
	}
	return &Iterator[K, V]{tree: tree, node: nil, position: end}
}

What did you see happen?

compile it get error.

What did you expect to see?

make compile pass through.

@seankhliao
Copy link
Member

you can only type switch on interfaces, once instantiated, K is a concrete type not an interface.

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 Nov 21, 2024
@seankhliao
Copy link
Member

Duplicate of #45380

@seankhliao seankhliao marked this as a duplicate of #45380 Nov 21, 2024
@golang golang locked as spam and limited conversation to collaborators Nov 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants