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

language semantics: Go doesn't enforce private types when using type deduction with := operator #59823

Closed
tnozicka opened this issue Apr 25, 2023 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. v2 A language change or incompatible library change
Milestone

Comments

@tnozicka
Copy link

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

$ go version
go version go1.20.1 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="/var/home/dev/.cache/go-build"
GOENV="/var/home/dev/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dev/dev/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/dev/dev/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/var/home/dev/dev/lib/golang/go1.20.1.src/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/var/home/dev/dev/lib/golang/go1.20.1.src/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/dev/null"
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 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3891585286=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

// Using private type works with type deduction (IMO it shouldn't have).
v := lib.Foo()

// Explicitly using the same type would fail to compile.
// var v lib.myType = lib.Foo()

Playground examples:
var v lib.myType = lib.Foo() - https://go.dev/play/p/YG1mv5bZCp2

./prog.go:14:12: myType not exported by package lib
./prog.go:14:21: cannot use lib.Foo() (value of type *lib.myType) as lib.myType value in variable declaration

v := lib.Foo() - https://go.dev/play/p/_XOR5LCDKUc

Result: &lib.myType{}

What did you expect to see?

Using private types should have failed with type deduction the same ways it does for explicit declarations of the same type.

What did you see instead?

Private types are enforced only with explicit declarations but are not enforced for deducted variable types.

@go101
Copy link

go101 commented Apr 25, 2023

Go has no "public type" and "private type" concepts.
Go only has "exported identifier" and "unexported identifier" concpets.
An unexported identifier can't be used (in code) outside of its containing package, but the type denoted by the unexported identifier may be used (in logic) outside of its containing package, because the type may have exported alias names.

@dmitshur dmitshur added v2 A language change or incompatible library change NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 25, 2023
@dmitshur dmitshur added this to the Go2 milestone Apr 25, 2023
@ianlancetaylor
Copy link
Contributor

As @go101 says: unexported is about identifiers, not types. This is not a bug, and it's not something we can or will change today. Thanks.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Apr 25, 2023
@zephyrtronium
Copy link
Contributor

See also #56669.

@golang golang locked and limited conversation to collaborators Apr 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants