What you are asking (to store 18,446,744,073,709,551,615 as an int64 value) is impossible.

A unit64 stores positive integers and has 64 bits available to hold information. It can therefore store any positive integer between 0 and 18,446,744,073,709,551,615 (2^64-1).

An int64 uses one bit to hold the sign, leaving 63 bits to hold information about the number. It can store any value between -9,223,372,036,854,775,808 and +9,223,372,036,854,775,807 (-2^63 and 2^63-1).

Both types can hold 18,446,744,073,709,551,616 unique integers, it is just that the uint64 range starts at zero, where as the int64 values straddle zero.

To hold 18,446,744,073,709,551,615 as a signed integer would require 65 bits.

In your conversion, no information from the underlying bytes is lost. The difference in the integer values returned is due to how the the two types interpret and display the values.

uint64 will display a raw integer value, whereas int64 will use two's complement.

var x uint64 = 18446744073709551615
var y int64 = int64(x)

fmt.Printf("uint64: %v = %#[1]x, int64: %v = %#x\n", x, y, uint64(y))
// uint64: 18446744073709551615 = 0xffffffffffffffff
// int64:                    -1 = 0xffffffffffffffff


x -= 100
y -= 100
fmt.Printf("uint64: %v = %#[1]x, int64: %v = %#x\n", x, y, uint64(y))
// uint64: 18446744073709551515 = 0xffffffffffffff9b
// int64:                  -101 = 0xffffffffffffff9b 

https://play.golang.com/p/hlWqhnC9Dh

Answer from PassKit on Stack Overflow
Discussions

Converting int32 to uint64 and back to int32
If your input is always int32, then you won't run into issues with converting between the 2. If your input is int64 you will definitely run into issues at some point. So, ensure your codepaths don't allow for user input of the wrong precision. More on reddit.com
🌐 r/golang
16
4
February 5, 2023
go - When casting an int64 to uint64, is the sign retained? - Stack Overflow
I am surprised, being new to Go, that after casting a negative number to uint64 that it remains negative -- I had planned to subtract the now positive value (after casting) to get what I wanted. ... Well, first uint(a) is not a cast in Go but a type conversion which just re-interprets the bits ... More on stackoverflow.com
🌐 stackoverflow.com
go - How to change int into int64? - Stack Overflow
Im trying to convert an integer into an integer64 in go but im having no luck. Anyone know an easy way to do this? More on stackoverflow.com
🌐 stackoverflow.com
Convert Uint64 to slice []Uint8
Hi all I’m using package satori/uuid to get uuid values But in my case after getting new uuid i need convert it to simple int type I found some solution but can’t understand how after work with int value reconvert it to uuid format again How to do it effective and simple? More on forum.golangbridge.org
🌐 forum.golangbridge.org
0
0
June 13, 2018
🌐
YourBasic
yourbasic.org › golang › convert-int-to-string
Convert between int, int64 and string · YourBasic Go
CODE EXAMPLE Use strconv.Itoa to convert an int to a decimal string, and strconv.Atoi to parse a string to an int.
🌐
Gopher Coding
gophercoding.com › convert-string-to-int
Convert String to Int, Int64, Uint64 · Gopher Coding
April 2, 2023 - This is one of those posts that won’t need a huge introduction. We want to convert a string into an integer. We’ll look at the different ways to do this (based on which sort of int you want). We’ll convert to int, uint and uint64 (though a int64 will be easy enough work out).
🌐
Reddit
reddit.com › r/golang › converting int32 to uint64 and back to int32
r/golang on Reddit: Converting int32 to uint64 and back to int32
February 5, 2023 -

I am a bit concerned about a piece of code I wrote. It seems to work but I did not quite expect it to...

How does type conversion exactly work in Go when I convert a int32 to uint64 and then back to int32. I was somewhat scared that funny business might happen.

To give practical context of what I am doing. I have data like:

type data struct {
    a uint16
    b uint8
    c uint8
    d int32
}

Instead of using a data struct I want to package all the values in a single uint64 like:

data := uint64(a) | uint64(b)<<16 | uint64(c)<<24 | uint64(d)<<32

and then unpack it like (shifting back and applying a bit mask if necessary):

d := int32(data>>32)

my worries were around:

data = uint64(int32(d))

I was worried that it would try to do some conversion from int32 with negative values. But running some tests all it seems to be doing is padding or truncating bits and interpreting the two's compliment int values as uint.

Is this all safe and correct or am I about to run into nightmarish bugs?

Edit: In essence what I am asking with go type conversions does this hold for every value of d of type int32 :

d == int32(uint64(d))

🌐
GoLinuxCloud
golinuxcloud.com › home › programming › how to change int to int64 in golang? [solved]
How to change int to int64 in Golang? [SOLVED] | GoLinuxCloud
January 8, 2023 - We can use the strconv.Itoa function to convert an int to a decimal string and then use the strconv.ParseInt function to parse a decimal string to an int64.
Top answer
1 of 1
7

Converting a signed number to an unsigned number will not remain negative, it can't, as the valid range of unsigned types doesn't include negative numbers. If you print uint(interval), you will certainly see a positive number printed.

What you experience is deterministic and you can rely on it (but it doesn't mean you should). It is the result of Go (and most other programming languages) storing signed integer types using the 2's completement representation.

What this means is that in case of negative numbers, using n bit, the value -x (where x is positive) is stored as the binary representation of the positive value 2^n - x. This has the advantage that numbers can be added bitwise, and the result will be correct regardless of whether they are negative or positive.

So when you have a signed negative number, it is basically stored in memory like if you would subtract its absolute value from 0. Which means that if you convert a negative, signed value to unsigned, and you add that to an unsigned value, the result will be correct because overflow will happen, in a useful way.

Converting a value of type int64 to uint64 does not change the memory layout, only the type. So what 8 bytes the int64 had, the converted uint64 will have those same 8 bytes. And as mentioned above, the representation stored in those 8 bytes is the bit pattern identical to the bit pattern of value 0 - abs(x). So the result of the conversion will be a number that you would get if you would subtract abs(x) from 0, in the unsigned world. Yes, this won't be negative (as the type is unsigned), instead it will be a "big" number, counting down from the max value of the uint64 type. But if you add an y number bigger than abs(x) to this "big" number, overflow will happen, and the result will be like y - abs(x).

See this simple example demonstrating what's happening (try it on the Go Playground):

a := uint8(100)
b := int8(-10)
fmt.Println(uint8(b)) // Prints 226 which is: 0 - 10 = 256 - 10
a = a + uint8(b)
fmt.Println(a)        // Prints 90 which is: 100 + 226 = 326 = 90
                      //           after overflow: 326 - 256 = 90

As mentioned above, you should not rely on this, as this may cause confusion. If you intend to work with negative numbers, then use signed types.

And if you work with a code base that already uses uint64 values, then do a subtraction instead of addition, using uint64 values:

interval := uint64(3600)
endTime -= interval

Also note that if you have time.Time values, you should take advantage of its Time.Add() method:

func (t Time) Add(d Duration) Time

You may specify a time.Duration to add to the time, which may be negative if you want to go back in time, like this:

t := time.Now()
t = t.Add(-3600 * time.Second)

time.Duration is more expressive: we see the value we specified above uses seconds explicitly.

Find elsewhere
🌐
Go Forum
forum.golangbridge.org › getting help
Convert Uint64 to slice []Uint8 - Getting Help - Go Forum
June 13, 2018 - Hi all I’m using package satori/uuid to get uuid values But in my case after getting new uuid i need convert it to simple int type I found some solution but can’t understand how after work with int value reconvert it …
🌐
Go Packages
pkg.go.dev › github.com › reiver › go-cast
cast package - github.com/reiver/go-cast - Go Packages
August 10, 2025 - Uint64 will return an int64 when `v` is of type uint64, uint32, uint16, uint8, uint or has a method:
🌐
Melvin George
melvingeorge.me › blog › convert-int-type-value-to-uint-type-value-go-golang
How to convert an int type value to a uint type value in Go or Golang? | MELVIN GEORGE
August 18, 2022 - To convert an int type value to a uint type value in Go or Golang, you can use the built-in uint() function and pass the int type value as an argument to the function.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › Y95NNox15Ns
overhead of type-casting
On Oct 7, 10:54 am, Andrew Gerrand <a...@golang.org> wrote: > Evan is correct about the conversion costs. > ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... On Wed, Oct 6, 2010 at 22:28, kar <akma...@gmail.com> wrote: > What's the overhead of type-casting integer in GO? (uint64 into > int64), since most of standard library's functions are not uniform
🌐
DEV Community
dev.to › melvin2016 › how-to-convert-an-int-type-value-to-a-uint-type-value-in-go-or-golang-531a
How to convert an int type value to a uint type value in Go or Golang? - DEV Community
August 18, 2022 - package main import "fmt" func main() { // a `int` type value intNum := 3000 // convert the `int` type value of 3000 // to `uint` type value using the // `uint()` built-in method uintNum := uint(intNum) // log to the console fmt.Println(uintNum) // `3000` and the type is `uint` }
🌐
Educative
educative.io › answers › what-is-type-int64-in-golang
What is type int64 in Golang?
These variables can hold many different data types, whether they are numbers, words, or any other type. To restrict the type of data stored inside these variables, we need to specify the data type of the variables. int is one of the available numeric data types in Go used to store signed integers.