Skip to content

I want to get member's reflect.Type via struct definition, but not it's instance. #34521

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
alswell opened this issue Sep 25, 2019 · 2 comments
Closed
Labels
FrozenDueToAge Unfortunate WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@alswell
Copy link

alswell commented Sep 25, 2019

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

$ go version
go version go1.10.4 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/lib/go-1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build414398923=/tmp/go-build -gno-record-gcc-switches"

What did you do?

type MyStruct struct {
	A int `json:"a"`
}
obj := MyStruct{} // I need to create an object of this struct first
fmt.Println(reflect.TypeOf(obj).Field(0).Tag.Get("json"))
fmt.Println(reflect.TypeOf(obj).Field(0).Type)

What did you expect to see?

I can get reflect.Type via 'MyStruct' itself, but not it's instance.
I only want to get the member's tag and type, if I need to create an object of this class first, it looks strange

What did you see instead?

I can get reflect.Type via 'MyStruct' itself, but not it's instance.
I only want to get the member's tag and type, if I need to create an object of this class first, it looks strange

@alswell alswell changed the title I can get member's reflect.Type via struct definition, but not it's instance. I want to get member's reflect.Type via struct definition, but not it's instance. Sep 25, 2019
@randall77
Copy link
Contributor

You can do this:

fmt.Println(reflect.TypeOf(MyStruct{}).Field(0).Tag.Get("json"))
fmt.Println(reflect.TypeOf(MyStruct{}).Field(0).Type)

It may look strange, but that's what you need to do to get a type. Types aren't first-class types in Go, you need to start with a value and ask reflect to find its type.

Do you have some concrete proposal for how it should work instead?

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 26, 2019
@mdempsky
Copy link
Contributor

mdempsky commented Oct 3, 2019

I don't think there's any action here.

@mdempsky mdempsky closed this as completed Oct 3, 2019
@golang golang locked and limited conversation to collaborators Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Unfortunate WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants