golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.21k stars 17.7k forks source link

template: golang can not get template type #70487

Closed epowsal closed 16 hours ago

epowsal commented 17 hours ago

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.

gabyhelp commented 17 hours ago

Related Issues

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

seankhliao commented 16 hours ago

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 commented 16 hours ago

Duplicate of #45380