Go Integers: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 19: Line 19:
[[Go Strings#Indexing_Operator|Indexing operator [] applied to strings]] return bytes.
[[Go Strings#Indexing_Operator|Indexing operator [] applied to strings]] return bytes.


=Conversion between <tt>byte</tt>s and <tt>string</tt>s=
=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:

Revision as of 06:15, 31 March 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.

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 = 90
str := string(b)

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