Makes a http get request using the blank identifier to ignore exceptions.
Simple code that declares a variable that is never used, its asign the var to a blank identifier to avoid declared and unused
error.
A program that calls two functions, one recive arguments by val and the other recive arguments by ref. It uses pointers to pass by ref.
Provides the capability to access a var only one process at a time. This code uses an atomic function to modify a variable and avoiding simultaneous access to it.
Shows how to share values through channels between two processes.
The optional <- operator specifies the channel direction, send or receive. If no direction is given, the channel is bidirectional. A channel may be constrained only to send or only to receive by conversion or assignment.
chan T // can be used to send and receive values of type T
chan<- float64 // can only be used to send float64s
<-chan int // can only be used to receive ints
Source: https://golang.org/ref/spec#Channel_types
How to use channels and WaitGroups in the same program.
What is a pipeline?
There's no formal definition of a pipeline in Go; it's just one of many kinds of concurrent programs. Informally, a pipeline is a series of stages connected by channels, where each stage is a group of goroutines running the same function. In each stage, the goroutin.
- receive values from upstream via inbound channels.
- perform some function on that data, usually producing new values.
- send values downstream via outbound channels.
Each stage has any number of inbound and outbound channels, except the - first and last stages, which have only outbound or inbound channels, respectively. The first stage is sometimes called the source or producer; the last stage, the sink or consumer.
Source: https://blog.golang.org/pipelines
An example of pipeline comunication pattern.
How to manage multiple threads using the same channel with no other library.
Is the composition of independently executing processes (dealing with a lot of things at once).
An example code that implements concurrency using the go keyword for creating goroutine and a WaitGroup
just for keeping the main thread alive until all other threads ends.
Is the simultaneously execution of (possibly related) computations (doing a lot of things at once).
An example code that shows how to implements parallelism using runtime.GOMAXPROCS()
function inside the init() function.
Example of a rance condition code and mutex for preventing unwanted behavio r.
Program that declares and use some constants, it also use the iota
value.
iota: a small amount of something
Code example that implements the uses for the for
statement.
- C/Java
for
style - Like
while
statement sustitution - Like
do
for infinite loops - With the
continue
keyword
Convertion: Transform a value of certain type to another (casting or parsing in other languajes). Assertion: Treats a value of one type as if it were another, without converting it.
Code that shows how to use assertion (only works with interfaces).
Code example that shows how to convert a typed value to another using strconv library.
Basic data structuresand how to deal with.
Example with an Array and a Slice and the differences between these types.
How to declare a slice and append elements to it.
Example code of maps declaration and manipulation.
This code shows how to create a custom type based on struct type, how to use it in a var declaration and how to change its field's values.
Uses some format verbs to display a number.
- Deciamal
- Binary
- Char
- Octal
- Hex (3 formats)
Uses a for to generate a numerical secuences and display every generated number in three formats.
- Decimal
%d
- Binary
%b
- Hex
%#X
- Char
%q
Code example that implements a for to generate a counter and a conditional (if) to clasificate generated numbers in odd and even using a module operation x % 2
.
Working with functions.
A simple code that shows how to declare and execute a anonymous function.
Example of a function call that needs a callback function to do certain things.
Example of three types of function's returns
- helloWorld: A simple (and traditionsl) single value return
- greet: A named var return
- names: A multiple values return
Code that uses the word defer
to make a function call be executed at the end of the current function.
Example of a function call that passes variadic arguments using the variadic technique.
Example of a function that can recive a unlimited number of arguments and stores in a collection, it's called variadic function.
Program with inner functions and functions that returns functions as result. Something like functions inception.
Some interfaces examples.
How to use empty interface to create a function that could manage any type of data/object.
Code that shows how to declare an interface and work with it.
Requirements:
Use https://godoc.org/sort to sortthe following:
(1)
type people []string
studyGroup := people{"Zeno", "John", "Al", "Jenny"}
(2)
s := []string{"Zeno", "John", "Al", "Jenny"}
(3)
n := []int{7, 4, 8, 2, 9, 19, 12, 32, 3}
Working with JSON
How to convert a streammed JSON text to an object.
Note: Use this for reading JSON through the web.
How to convert an object to a JSON string to write it on a stream.
Note: Use this for sending Objects JSON through the web.
Having a string with JSON content, this code will assign its values to an instance of a type that has the same exported fields using json.unmarshal()
method.
Having a custom class object(struct), this code will convert it to a JSON formated string using json.Marshal()
method.
A wey to get var's memmory address, good introduction to pointers.
Example of nested loops, prints values of both vars.
How to use the ìmport
statement for multiple libs.
How to declares and use pointers in GO.
runes generator example rune = char
, rune type is an alias of int32.
Example code using switch statements.
An example of how to implement a type based switch function.
A program that defines twoo custom types using struct, one inside the other; both of them has a method with the same name that the code calls for each type, it also contains a function that receive a pointer to a custom type.
Example about the both ways to declare a variable.
How does scopes works on Go:
Main code, only imports a lib and calls a functions
Has two files in the same package, one has the var declaration and the other has the function that prints text messages.