🐨 Go lang Study Repository.
A little bit of the basics.!
- Programs in go are made up of
packages
. - Module imports are done using the
import
key and can be factored using parentheses()
to group multiple imports. - Named exports are done using campitalyzed words 'SameExample',
- Functions take one or more arguments where the type follows the argument name!
- When arguments share the same type, I can omit all and preserve only the last
x int, y int
forx, y int
. - Function can return one or several results.
- Variables can be declared using the
var
key egvar nameVariable type
. - Variables can be initialized using
var nameVariable = value
. In this case the type can be omitted as the variable will take the type of the initiator. - Inside a function I can use short variables using
:=
, but outside the functions I must use thevar
key. - The basic types are
bool
,string
,byte
,rune
,float32
,float64
. - Zero values are,
0 for numbers
,false for booleans
and"" for strings
. - When a variable is declared without specifying the type, the type is defined according to the assigned value.
- Constants are declared using the
const
key and values cannot be assigned using:=
. - Go has only one type of loop, the
for
. - Reference
Hello world in GO!
- Create a module using the
go init
command. - Declare a package.
- Import standard packages.
- Importing external packages.
- Synchronize dependencies using the
go mod tidy
command. - Reference | Commit
Creating a module!
- Declaring a function, as well as the type of parameter and return.
- I learned that functions with a capital initial can be called by a function that is not in the same package.
- I learned how to declare a variable and assign the value in one line using
:=
. - Formatting a string using the
Sprintf
function. - Reference | Commit
Import a local module!
- Import a local module.
- Use the
go mod edit -replace module=directory
command to reference a local module. - Reference | Commit
Validations and error handling!
- How a function can return multiple values.
- Use the
errors
module to throw exceptions. - Use the
log
module to display errors in the terminal. - Assign value to a variable already declared using
=
. - Reference | Commit
Get random values based on a condition!
- The use of the
init
function to execute functions at startup. - Declare a
slice/array
. - Get the size of a data using the
len
function. - Use the
Seed
function of therand
module to initialize the seeds on each run. - Use the
Intn
function of therand
module to generate a random number within the specified maximum size. - Reference | Commit
Iterate a slice and populate a map!
- Declare a
map
and initialize using themake
function. - Use the
for-range
loop to iterate over amap/slice/array
. - Use the blank
_
identifier to ignore the value. - Use a generic type with
interface {}
. - Reference | Commit
Unitary tests!
- A test file must be named with the suffix
_test.go
. - The test function must start with
Test
name. - The test function must take a single
t *testing.T
argument providing all the necessary functions for the test. - Use
go test
command to run. - Use the
testing
module. - Use
regexp
module to work with regex. - Reference | Commit