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

proposal: encoding/json: add Int() and Int32() methods on json.Number #23573

Closed
wgliang opened this issue Jan 26, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@wgliang
Copy link
Contributor

commented Jan 26, 2018

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:

func (n Number) Int() (int, error)
func (n Number) Int32() (int32, error)

What did you see instead?

@bradfitz bradfitz changed the title encoding/json: Add Int() and Int32() interfaces of json.Number proposal: encoding/json: add Int() and Int32() methods on json.Number Jan 26, 2018

@gopherbot gopherbot added this to the Proposal milestone Jan 26, 2018

@gopherbot gopherbot added the Proposal label Jan 26, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jan 26, 2018

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?

@wgliang

This comment has been minimized.

Copy link
Contributor Author

commented Jan 27, 2018

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.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jan 27, 2018

What about my other question: why should we add more methods?

@robpike

This comment has been minimized.

Copy link
Contributor

commented Jan 29, 2018

We should not.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Jan 29, 2018

Use strconv.Atoi(string(n)) instead of the non-existent n.Int.

@rsc rsc closed this Jan 29, 2018

@golang golang locked and limited conversation to collaborators Jan 29, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.