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

false error when inserting an interface into a map #39911

Closed
nektro opened this issue Jun 29, 2020 · 6 comments
Closed

false error when inserting an interface into a map #39911

nektro opened this issue Jun 29, 2020 · 6 comments

Comments

@nektro
Copy link

nektro commented Jun 29, 2020

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

$ go version
go version go1.14.3 linux/amd64

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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/meghan/.cache/go-build"
GOENV="/home/meghan/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/meghan/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build301010850=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/3zMyaRx10_z

package main

import (
	"fmt"
	"hash"
	"hash/adler32"
)

var (
	algoMap = map[string]func() hash.Hash{}
)

func main() {
	algoMap["adler32"] = adler32.New

	_, ok := algoMap["adler32"]
	fmt.Println(ok)
}

What did you expect to see?

true

What did you see instead?

./prog.go:14:21: cannot use adler32.New (type func() hash.Hash32) as type func() hash.Hash in assignment

@nektro
Copy link
Author

nektro commented Jun 29, 2020

yet hash.Hash32 implements hash.Hash (https://golang.org/pkg/hash/#Hash32)

image

@shawndx
Copy link
Contributor

shawndx commented Jun 29, 2020

Just my two cents here:

According to Go's assignment semantic (https://golang.org/ref/spec#Assignability), functions are expected to be of identical type in assignment context, even though hash.Hash32 is indeed assignment compatible to hash.Hash.

@antong
Copy link
Contributor

antong commented Jun 29, 2020

(func() hash.Hash32) and (func() hash.Hash) are distinct, non-interface types. That Hash32 happens to implement Hash doesn't make it possible to assign a value of the first type to a variable of the other type. You can assign a value of Hash32 to a variable of type Hash, but you can not assign a value of type (func() hash.Hash32) to a variable of type (func() hash.Hash).

@nektro
Copy link
Author

nektro commented Jun 29, 2020

According to Go's assignment semantic (golang.org/ref/spec#Assignability), functions are expected to be of identical type in assignment context, even though hash.Hash32 is indeed assignment compatible to hash.Hash.

This is rather unfortunate as I feel it should be an allowed assignment, though since the behavior is documented I'm willing to close the issue if this ends up being the final say.

@nektro
Copy link
Author

nektro commented Jun 29, 2020

You can assign a value of Hash32 to a variable of type Hash, but you can not assign a value of type (func() hash.Hash32) to a variable of type (func() hash.Hash).

This is the root if why I believe this is an error.

@ianlancetaylor
Copy link
Contributor

The compiler is behaving correctly according to the language definition, so closing the issue. See https://golang.org/doc/faq#covariant_types. For further discussion of why the language works this way, please use a forum; see https://golang.org/wiki/Questions. Thanks.

@golang golang locked and limited conversation to collaborators Jun 29, 2021
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

5 participants