proposal: encoding/json: add Int() and Int32() methods on json.Number #23573
Labels
Milestone
Comments
As you say, you can just write a conversion. It doesn't even take an extra line of code, just a few more characters. Why should we add more methods? And if we add more, why shouldn't we add methods for all the numeric types? |
I think we should add methods for all the numeric types. We have it as a number type, so the interface itself should support direct access to its corresponding type value instead of letting the user cast it to the type he needs. |
What about my other question: why should we add more methods? |
We should not. |
Use strconv.Atoi(string(n)) instead of the non-existent n.Int. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.9.2 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=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/wangguoliang/Documents/mos"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zd/20dl0mb523v4tjk_pf_hdscr0000gn/T/go-build194310903=/tmp/go-build -gno-record-gcc-switches -fno-common"
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"
What did you do?
json.Number supports both the Int64 () and Float64 () interfaces, and I think I should support other types, such as Int () and Int32, though the user himself can use the Int64 interface through conversions.
Related PR
cc @bradfitz
What did you expect to see?
encoding/json package.
I want support Built-in interface:
What did you see instead?
The text was updated successfully, but these errors were encountered: