Go Integers: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
* Unsigned integers: <tt>uint8</tt> (or <tt>byte</tt>), <tt>uint16</tt>, <tt>uint32</tt> (or <tt>rune</tt>), <tt>uint64</tt>.
* Unsigned integers: <tt>uint8</tt> (or <tt>byte</tt>), <tt>uint16</tt>, <tt>uint32</tt> (or <tt>rune</tt>), <tt>uint64</tt>.
* Signed integers: <tt>int8</tt>, <tt>int16</tt>, <tt>int32</tt>, <tt>int64</tt>.
* 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>.
* Machine-dependent integers: <tt>uint</tt>, <tt>int</tt> and <tt>uintptr</tt>. <tt>int</tt> for example can be 4 bytes or 8 bytes long, depending on the underlying architecture.


When a regular integer is needed in the program, <tt>int</tt> should be the default.
When a regular integer is needed in the program, <tt>int</tt> should be the default.

Revision as of 18:31, 12 April 2016

Internal

Overview

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

  • Unsigned integers: uint8 (or byte), uint16, uint32 (or rune), uint64.
  • Signed integers: int8, int16, int32, int64.
  • 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.

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")

The following expression converts a slice of bytes to string:

str := string(bytes)

Also see:

Conversion between Types