Go Basics
title: “Golang - Basics” date: "" tags: ["", “”] keywords: ["", “”] description: ""
Basic things to remember while bouncing around languages
Arrays exist in Golang and can be created manually:
However, nearly all usages of arrays are through slices, which are built on top of arrays, providing a view into a backing array.
Some things to keep in mind:
- Changing a value in a slice changes the value in the backing array
- Ranges can be used on slices:
slice[1:5], slice[:2], slice[2:]
- A slice can be resized to reflect more or less of the underlying array, through assignment:
slice = slice[:cap(slice)]
- Resizing a slice past the capacity of the array will throw a panic
You can define an array of basic types use a compose literal
like so:
|
|
Pacakge sort
is available, with common sorting functions for slices
- A sorting algorithm is can be supplied using
sort.Slice
|
|
case
statements with switches do not fall through, as with other C style languages!default
is supportedswitch
can be used with or without an argumentcase
can evaluate expressions
|
|
- You can do multiplie assignments of variables of in a for loop if you’re into that kind of thing
|
|
|
|
Result:
|
|
Note the ususal issue with truncating a float to an int based number, possible unexpected rounding errors.