Thanks to visit codestin.com
Credit goes to github.com

Skip to content

U/Int64 conversion  #360

Closed
Closed
@sirudog

Description

@sirudog

Hi,
Recently I came across an issue of javascript not being able to handle numbers larger than 2^53 - 1.
This is because javascript number type is specified as an IEEE754 floating-point value, so it is not guaranteed to be able to hold a 64-bit unsigned value. Floating-point values can only represent values up to 2^53 - 1 without losing precision.
So I had to use a js library to use 64 bit integers correctly and safely. (there are several libs for this out there by the way, I chose bignumber.js (https://github.com/MikeMcl/bignumber.js))

Based on these definitions found in go.js (https://github.com/gopherjs/gopherjs/blob/master/js/js.go)

// Int64 returns the object converted to int64 according to JavaScript type conversions (parseInt).
func (o *Object) Int64() int64 { return o.object.Int64() }

// Uint64 returns the object converted to uint64 according to JavaScript type conversions (parseInt).
func (o *Object) Uint64() uint64 { return o.object.Uint64() }

it is not clear if I need to take care of this, or it is being taken care of by gopherjs, and if so, how this conversion is being done, and what would be the proper way to handle these following scenarios:

  1. JS => Go
    function in Go:
    func TakeMyInt64(myInt64 uint64) { ... }
    when I call it from JS, I may not pass an integer of type number directly to this function, as I have to use a custom uint64 object from a JS library to handle 64 bit ints correctly.
  2. Go => JS
    function in Go:
    func GetInt64() uint64 { ... }
    when I call it from JS, what will I get back? Does gopherjs use its own internal type object to represent 64 bit integers? Is this object what I have found declared in the generated JS code?
var $Uint64        = $newType( 8, $kindUint64,        "uint64",         "uint64",     "", null);

It looks like I will have to do a conversion between $Uint64 (GoJS) and BigNumber (bignumber.js lib) objects.
Any help/opinion is much appreciated

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions