Inspired by Learn X in Y.

Note that this import this needed for the prints to work.

import (
	"fmt"
)

Note: Unlike Java or C, in Go the variable type comes after the variable name.

Numeric

// Declare an int without specifying bits or signed/unsigned.
var x int
// Reassign.
x = 0

// Declare and assign as unsigned int.
var u uint = 7

// Inferred type. The var keyword is not needed and colon is used.
y := 1
var my_float float32
var my_float2 float64 = 0.1
my_float2 := 1.234
my_float3 := 1.

Strings

Basic:

s := "My string"

Raw string - useful for multi-line or when containing quotes.

raw_str := `My raw
string`

s := `ffprobe : -i "/media/Name of File.mp3" : -show_entries format=duration : -v quiet : -of csv=p=0`

Unicode:

unicode_utf8_str = 'Σ'

Size of numeric tpyes

Copied from: tutorial.

Go has the following architecture-independent unsigned and signed integer types:

Type Range
uint8 0 to 255
uint16 0 to 65535
uint32 0 to 4294967295
uint64 0 to 18446744073709551615
int8 -128 to 127
int16 -32768 to 32767
int32 -2147483648 to 2147483647
int64 -9223372036854775808 to 9223372036854775807

Floats and complex numbers also come in varying sizes:

Type Description
float32 IEEE-754 32-bit floating-point numbers
float64 IEEE-754 64-bit floating-point numbers
complex64 Complex numbers with float32 real and imaginary parts
complex128 Complex numbers with float64 real and imaginary parts

See IEEE-754 and Double precision floating.

There are also a couple of alias number types, which assign useful names to specific data types:

Type Description
byte alias for uint8
rune alias for int32

A byte is the type of a character, unless it is a unicode character then it is rune.

e.g.

x := 'Σ'