My smart contract has a function like this

function setTest(uint256 test) returns (bool){
    testNumber = test;
    return true;

I used these piece of code Go to create a transaction to set new value to testNumber - a RPC call

var newtest uint256 = 100 // I expected this type (uint256)
abis, err := abi.JSON(strings.NewReader(contractInterface))
output, err := abis.Pack("setTest", newtest)

params := make(map[string]string)
params["data"] = fmt.Sprintf("0x%s", common.Bytes2Hex(output))
params["from"] = self.ownerAccount
params["to"] = self.contractAccount

done := ""
err = self.client.Call(&done, "eth_sendTransaction", params)

And the problem is I can't set newtest as type uint256. I can't find type uint256 in Go. Please help me, generate number type uint256 or convert from (big.Int, int64, uint64) to uint256. Thank you


One of the Golang gurus might want to comment further, but I don't think Go supports 256-bit integers natively.

You could import Geth's number package, together with math/big, and use the relevant parts.

e.g. Call Uint256():

// Return a Number with a UNSIGNED limiter up to 256 bits
func Uint256(n int64) *Number {
    return &Number{big.NewInt(n), limitUnsigned256}

Which returns a pointer to a structure that can pretend to represent a number that large:

// A Number represents a generic integer with a bounding function limiter. Limit is called after each operations
// to give "fake" bounded integers. New types of Number can be created through NewInitialiser returning a lambda
// with the new Initialiser.
type Number struct {
    num   *big.Int
    limit func(n *Number) *Number

You might want to consider why you're using a 256-bit type in first place, as the above seems a bit of a kludge.

  • As of October 2018, there is no number package nor a Uint256 function in geth. How would you do that now? – Paul Razvan Berg Oct 15 '18 at 23:37
  • @PaulRBerg do go get the repository, then checkout the old commit. or better to vendor it. – novalagung Oct 16 '18 at 0:38
  • @xpare I see that may work but why doesn't the current implementation with U256 work? The number of bytes of math.U256(big.NewInt(5)) seems to be 1. – Paul Razvan Berg Oct 16 '18 at 1:13
  • I was just trying to answer your question "How would you do that now?". I haven't even go get the package. – novalagung Oct 16 '18 at 2:58

Not sure since when exactly, but, as of October 2018, Richard's answer is outdated. You can generate a uint256 by importing the github.com/ethereum/go-ethereum/accounts/abi package and then doing:

var number []byte
number = abi.U256(big.NewInt(3))

Testing it:

fmt.Printf("value: %d\n", number)
fmt.Printf("number of bytes: %d", len(number))

Output is:

value: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3]

number of bytes: 32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.