int or int64

180 views
Skip to first unread message

JohnGB

unread,
Aug 14, 2015, 9:06:21 PM8/14/15
to golang-nuts
I understand what an int is and that it can vary depending on the system.  However, it's not clear to me (besides the obvious cases where only an int64 will suffice) which I should favour, and in which situations I should use one or the other.

For example:

- I have a list of objects (currently 4k), but likely to grow.  Should I use an int or an int64 for the (sequential) IDs?
- I want to store the order of s few thousand items. Do I store the ordinal value of an item as an int or int64?

I can see arguments both ways, but I'd like feedback on what the idiomatic preference is, and hopefully a little explanation of why.

Roberto Zanotto

unread,
Aug 14, 2015, 9:32:37 PM8/14/15
to golang-nuts
I'd say just use int for array indices and such. It should always be enough, since in 32 bit systems you can't address more than 4GB of bytes, which implies that you'll have arrays and lists shorter than 4GB, which means that normal int is sufficient.
I think that the idiomatic way is: don't even care specifying the type in most cases, like:
n := 50
for i := 0; i < n; i++ {
   
...
}
Reply all
Reply to author
Forward
0 new messages