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

runtime: reflect string slice,the element type is interface{} #23149

Closed
nzlov opened this issue Dec 15, 2017 · 1 comment
Closed

runtime: reflect string slice,the element type is interface{} #23149

nzlov opened this issue Dec 15, 2017 · 1 comment

Comments

@nzlov
Copy link

@nzlov nzlov commented Dec 15, 2017

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

go version devel +94d7c884c3 Thu Dec 14 14:57:01 2017 +0000 darwin/amd64

Does this issue reproduce with the latest release?

YES

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/qipeng/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/qipeng/workspace/go"
GORACE=""
GOROOT="/Users/qipeng/program/go"
GOTMPDIR=""
GOTOOLDIR="/Users/qipeng/program/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bs/9120vpt14jd7rkhg27kdbj480000gn/T/go-build475790858=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

func main() {
    str := `
    {
        "list":[0,1,2]
    }
  `
    m := map[string]interface{}{}
    err := json.Unmarshal([]byte(str), &m)
    if err != nil {
         panic(err)
    }
    fmt.Println(reflect.ValueOf(m["list"]).Index(1).Type())
}

What did you expect to see?

int64 or float64

What did you see instead?

interface {}

@bradfitz

This comment has been minimized.

Copy link
Contributor

@bradfitz bradfitz commented Dec 15, 2017

This is working as intended, per the encoding/json docs:

https://golang.org/pkg/encoding/json/#Unmarshal

Which says:

[]interface{}, for JSON arrays

If you want a more specific type, you need to define a struct to unmarshal into.

@bradfitz bradfitz closed this Dec 15, 2017
@mikioh mikioh changed the title runtime:reflect string slice,the element type is interface{} runtime: reflect string slice,the element type is interface{} Jan 5, 2018
@golang golang locked and limited conversation to collaborators Jan 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.