🐨 Go lang Study Repository.
A little bit of the basics.!
- Programs in go are made up of
packages. - Module imports are done using the
importkey 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 intforx, y int. - Function can return one or several results.
- Variables can be declared using the
varkey 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 thevarkey. - The basic types are
bool,string,byte,rune,float32,float64. - Zero values are,
0 for numbers,false for booleansand"" 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
constkey 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 initcommand. - Declare a package.
- Import standard packages.
- Importing external packages.
- Synchronize dependencies using the
go mod tidycommand. - 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
Sprintffunction. - Reference | Commit
Import a local module!
- Import a local module.
- Use the
go mod edit -replace module=directorycommand to reference a local module. - Reference | Commit
Validations and error handling!
- How a function can return multiple values.
- Use the
errorsmodule to throw exceptions. - Use the
logmodule 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
initfunction to execute functions at startup. - Declare a
slice/array. - Get the size of a data using the
lenfunction. - Use the
Seedfunction of therandmodule to initialize the seeds on each run. - Use the
Intnfunction of therandmodule to generate a random number within the specified maximum size. - Reference | Commit
Iterate a slice and populate a map!
- Declare a
mapand initialize using themakefunction. - Use the
for-rangeloop 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
Testname. - The test function must take a single
t *testing.Targument providing all the necessary functions for the test. - Use
go testcommand to run. - Use the
testingmodule. - Use
regexpmodule to work with regex. - Reference | Commit