Strings
str := "Hello"
Multiline string
str := `Multiline
string`
This shows regular strings and raw multiline strings. Raw strings are handy when you want to keep formatting as written.
Numbers
Typical types
num := 3 // int
num := 3. // float64
num := 3 + 4i // complex128
num := byte('a') // byte (alias for uint8)
Other Types
var u uint = 7 // uint (unsigned)
var p float32 = 22.7 // 32-bit float
Go keeps numeric types explicit. The examples show the common defaults plus a few typed variants when precision or size matters.
Arrays
// var numbers [5]int
numbers := [...]int{0, 0, 0, 0, 0}
This creates a fixed-size array. In day-to-day Go, slices are more common, but arrays are the underlying fixed-length value.
Pointers
func main () {
b := *getPointer()
fmt.Println("Value is", b)
func getPointer () (myPointer *int) {
a := 234
return &a
a := new(int)
*a = 234
Pointers point to a memory location of a variable. Go is fully garbage-collected.
Pointers let you work with the same value across functions without copying it. Go keeps them simpler than C by handling memory cleanup for you.
Type Conversion
i := 2
f := float64(i)
u := uint(i)
Go does not do many implicit numeric conversions, so converting types directly like this is normal.
Slice
slice := []int{2, 3, 4}
slice := []byte("Hello")
Slices are the main sequence type in Go. They are lightweight views over array-backed data and are used much more than arrays.