Go Integers: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(50 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://go.dev/ref/spec#Numeric_types
=Internal=
=Internal=
* [[Go Language#Integers|Go Language]]
* [[Go Strings]]


* [[Go Concepts - The Type System#Built-in_Types|Built-in Types]]
=Overview=
Integers are designated by the following [[Go_Language#Pre-Declared_Types|pre-declared type identifiers]].
 
In most cases, we simply declare an <code>int</code> (signed integer) or <code>uint</code> (unsigned integer) and leave it to the compiler to allocate the right type of integer, depending on the underlying architecture. If we want to get specific, we can use:
* Unsigned integers represented on the specified number of bits: <code>uint8</code> (<code>byte</code>), <code>uint16</code>, <code>uint32</code> (<span id='Rune'></span><span id='rune'></span><code>rune</code>, also see [[Go_Strings#Rune|strings]]), <code>uint64</code>.
* Signed integers, represented on the specified number of bits: <code>int8</code>, <code>int16</code>, <code>int32</code>, <code>int64</code>. Internally they are stored in [[Two's Complement Representation|2's complement representation]].
* Machine-dependent integers: <code>uint</code>, <code>int</code> and <code>uintptr</code>. <code>int</code>, for example, can be 4 bytes or 8 bytes long, depending on the underlying architecture.
 
When a regular integer is needed in the program, <code>int</code> should be the default.
 
The uninitialized value is <code>0</code>.
 
=Integer Variable Declaration=
<syntaxhighlight lang='go'>
var i int        // string type declaration without initialization
i = 1            // initialization after declaration
var i2 int = 2    // variable initialization in declaration
var i3 = 3        // variable initialization with type inference
i4 := 4          // short variable declaration
</syntaxhighlight>
 
=Max and Min Values=
 
Available as costs in the <code>math</code> package:
<syntaxhighlight lang='go'>
const (
MaxInt    = 1<<(intSize-1) - 1  // MaxInt32 or MaxInt64 depending on intSize.
MinInt    = -1 << (intSize - 1) // MinInt32 or MinInt64 depending on intSize.
MaxInt8  = 1<<7 - 1            // 127
MinInt8  = -1 << 7            // -128
MaxInt16  = 1<<15 - 1          // 32767
MinInt16  = -1 << 15            // -32768
MaxInt32  = 1<<31 - 1          // 2147483647
MinInt32  = -1 << 31            // -2147483648
MaxInt64  = 1<<63 - 1          // 9223372036854775807
MinInt64  = -1 << 63            // -9223372036854775808
MaxUint  = 1<<intSize - 1      // MaxUint32 or MaxUint64 depending on intSize.
MaxUint8  = 1<<8 - 1            // 255
MaxUint16 = 1<<16 - 1          // 65535
MaxUint32 = 1<<32 - 1          // 4294967295
MaxUint64 = 1<<64 - 1          // 18446744073709551615
)
</syntaxhighlight>


=Overview=
<syntaxhighlight lang='go'>
import math
 
...
math.MaxInt
</syntaxhighlight>
 
=Integer Literals=


Integers are designated by the following [[Go Concepts - Lexical Structure#Pre-Declared_Identifiers|pre-declared type identifiers]]:
=Operators=
==Binary Operators==
Arithmetic: <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>, <code><<</code>, <code>>></code>


* Unsigned integers: <tt>uint8</tt> (or <tt>byte</tt>), <tt>uint16</tt>, <tt>uint32</tt> (or <tt>rune</tt>), <tt>uint64</tt>.
Comparison: <code>==</code>, <code>!=</code>, <code>></code>, <code><</code>, <code>>=</code>, <code><=</code>
* Signed integers: <tt>int8</tt>, <tt>int16</tt>, <tt>int32</tt>, <tt>int64</tt>.
* Machine-dependent integers: <tt>uint</tt>, <tt>int</tt> and <tt>uintptr</tt>.


When a regular integer is needed in the program, <tt>int</tt> should be the default.
Boolean: <code>&&</code>, <code>||</code>


=<tt>byte</tt>=
=<tt>byte</tt>=


<tt>byte</tt> is unsigned int represented on a byte (<tt>uint8</tt>).  
<code>byte</code> is unsigned int represented on a byte (<code>uint8</code>).  


[[Go Strings#Indexing_Operator|Indexing operator [] applied to strings]] return bytes.
[[Go Strings#Indexing_Operator|Indexing operator [] applied to strings]] return bytes.


=Conversion of a <tt>byte</tt> to <tt>string</tt>=
=Conversion between bytes and strings=


A single byte can be converted to string as follows:
A single byte can be converted to string as follows:
<pre>
<syntaxhighlight lang='go'>
var b byte = 90
var b byte = 65 // decimal ASCII for "A"
str := string(b)
str := string(b) // str is "A"
</pre>
</syntaxhighlight>


The following expression converts a string to a [[Go Slices|slice]] of bytes:
The following expression converts a string to a [[Go Slices|slice]] of bytes:


<pre>
<syntaxhighlight lang='go'>
bytes := []byte("test")
bytes := []byte("test")
</pre>
</syntaxhighlight>
<syntaxhighlight lang='go'>
rawJSON := []byte(`{
  "something": "something else",
  "colors": [
    "red",
    "blue",
    "green"
  ]
}`)
</syntaxhighlight>
The following expression converts a slice of bytes to string:
 
<syntaxhighlight lang='go'>
str := string(bytes)
</syntaxhighlight>


Conversion of a byte slice to <tt>string</tt>:
Also see:


<pre>
{{Internal|Go_Concepts_-_The_Type_System#Conversion_Between_Types|Conversion between Types}}
str := string([]byte{'t', 'e', 's', 't'})
{{Internal|Go_Strings#String_Conversions|String Conversions}}
</pre>


Also see:
=Integers as characters=
 
<syntaxhighlight lang='go'>
i := 'A'
j := i + 1
fmt.Printf("%c %d\n", j, j)
</syntaxhighlight>
 
Also see: {{Internal|Go_Printing_to_Stdout_and_Stderr#fmt.Printf.28.29|<tt>fmt.Printf()</tt>}}


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
=Integers to Strings=
:[[Go_Concepts_-_The_Type_System#Conversion_Between_Types|Conversion between Types]]
<syntaxhighlight lang='go'>
</blockquote>
i := 0
string(48 + i)
</syntaxhighlight>

Latest revision as of 02:14, 12 June 2024

External

Internal

Overview

Integers are designated by the following pre-declared type identifiers.

In most cases, we simply declare an int (signed integer) or uint (unsigned integer) and leave it to the compiler to allocate the right type of integer, depending on the underlying architecture. If we want to get specific, we can use:

  • Unsigned integers represented on the specified number of bits: uint8 (byte), uint16, uint32 (rune, also see strings), uint64.
  • Signed integers, represented on the specified number of bits: int8, int16, int32, int64. Internally they are stored in 2's complement representation.
  • Machine-dependent integers: uint, int and uintptr. int, for example, can be 4 bytes or 8 bytes long, depending on the underlying architecture.

When a regular integer is needed in the program, int should be the default.

The uninitialized value is 0.

Integer Variable Declaration

var i int         // string type declaration without initialization
i = 1             // initialization after declaration
var i2 int = 2    // variable initialization in declaration
var i3 = 3        // variable initialization with type inference
i4 := 4           // short variable declaration

Max and Min Values

Available as costs in the math package:

const (
	MaxInt    = 1<<(intSize-1) - 1  // MaxInt32 or MaxInt64 depending on intSize.
	MinInt    = -1 << (intSize - 1) // MinInt32 or MinInt64 depending on intSize.
	MaxInt8   = 1<<7 - 1            // 127
	MinInt8   = -1 << 7             // -128
	MaxInt16  = 1<<15 - 1           // 32767
	MinInt16  = -1 << 15            // -32768
	MaxInt32  = 1<<31 - 1           // 2147483647
	MinInt32  = -1 << 31            // -2147483648
	MaxInt64  = 1<<63 - 1           // 9223372036854775807
	MinInt64  = -1 << 63            // -9223372036854775808
	MaxUint   = 1<<intSize - 1      // MaxUint32 or MaxUint64 depending on intSize.
	MaxUint8  = 1<<8 - 1            // 255
	MaxUint16 = 1<<16 - 1           // 65535
	MaxUint32 = 1<<32 - 1           // 4294967295
	MaxUint64 = 1<<64 - 1           // 18446744073709551615
)
import math

...
math.MaxInt

Integer Literals

Operators

Binary Operators

Arithmetic: +, -, *, /, %, <<, >>

Comparison: ==, !=, >, <, >=, <=

Boolean: &&, ||

byte

byte is unsigned int represented on a byte (uint8).

Indexing operator [] applied to strings return bytes.

Conversion between bytes and strings

A single byte can be converted to string as follows:

var b byte = 65 // decimal ASCII for "A"
str := string(b) // str is "A"

The following expression converts a string to a slice of bytes:

bytes := []byte("test")
rawJSON := []byte(`{
  "something": "something else",
  "colors": [
     "red",
     "blue",
     "green"
  ]
}`)

The following expression converts a slice of bytes to string:

str := string(bytes)

Also see:

Conversion between Types
String Conversions

Integers as characters

i := 'A'
j := i + 1
fmt.Printf("%c %d\n", j, j)

Also see:

fmt.Printf()

Integers to Strings

i := 0
string(48 + i)