Go es el lenguaje de programación de Google lanzado en 2009, este está disponible para diferentes tipos de sistemas Unix-like, incluidos FreeBSD, Linux, Mac OS X y Plan 9 además de para distintas arquitecturas como i386, amd64 y ARM.
Las principales ventajas respecto a otros lenguajes de programacón son:
- Mayor rendimiento que Python, Ruby o JavaScript.
- Mejor soporte para trabajos concurrentes.
- Go runtime incluÃdo en los binarios lo que lo hace altamente distribuÃble entre sistemas.
- LibrerÃas descentralizadas, cada programador hospeda su código donde desee, para instalar el software se accede a dicha plataforma directamente.
-
Instalación
-
Hello world
-
Variables y tipos de datos
-
Estructuras de control de flujo
-
Funciones
-
Arrays y slices
-
Maps
-
Recursos adicionales
Instalación:
pkg install go
Hello world:
vi 00-HelloWorld.go
// Define as executable programme not a library.
package main
// Format library
import "fmt"
func main() {
fmt.Println("Hello world!")
}
go run 00-HelloWorld.go
Hello world!
go build 00-HelloWorld.go
file 00-HelloWorld
00-HelloWorld: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically linked, for FreeBSD 12.3, FreeBSD-style, Go BuildID=Y4MQLyWJvSerAP4BVk2v/9P2KHgW0r9hY_WmEb9uy/zyzxSbQ1ETRoacqPL2Kq/-SzOfM95EQBcBz8mFnZr, with debug_info, not stripped
./00-HelloWorld
Hello world!
Variables y tipos de datos:
vi 01-Variables.go
// Define as executable programme not a library.
package main
// Format library
import "fmt"
func main() {
// Variable declaration
var x int
x = 5
// Short declaration method, auto variable type detection
y := 10
// Imprimir variables
fmt.Println("x:", x)
fmt.Println("y:", y)
}
go run 01-Variables.go
x: 5
y: 10
Estructuras de control de flujo:
vi 02-FlowControl.go
// Define as executable programme not a library.
package main
// Format library
import "fmt"
func main() {
// Conditionals
age := 25
if age >= 18 {
fmt.Println("You are on legal age")
} else {
fmt.Println("You are NOT on legal age")
}
// For loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
}
go run 02-FlowControl.go
You are on legal age
0
1
2
3
4
Funciones:
vi 03-Functions.go
// Define as executable programme not a library.
package main
// Format library
import "fmt"
// Function that adds two numbers
func add(a, b int) int {
return a + b
}
func main() {
result := add(3, 4)
fmt.Println("Result:", result)
}
go run 03-Functions.go
Result: 7
Arrays y slices:
vi 04-Arrays_Slices.go
// Define as executable programme not a library.
package main
// Format library
import "fmt"
func main() {
// Array
var numbers [5]int
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4
numbers[4] = 5
// Slice: We generate an array with the sub-content of another array
// From position: numbers[1] to numbers[4-1], the last position is not included
slice := numbers[1:4]
fmt.Println("Array:", numbers)
fmt.Println("Slice:", slice)
}
go run 04-Arrays_Slices.go
Array: [1 2 3 4 5]
Slice: [2 3 4]
Maps:
vi 05-Maps.go
// Define as executable programme not a library.
package main
// Format library
import "fmt"
func main() {
// Map declaration
person := map[string]string{
"name": "John",
"surname": "Doe",
"age": "30",
}
fmt.Println("Name:", person["name"])
// Add map field
person["city"] = "New York"
// Print the map
fmt.Println("Person:", person)
}
go run 05-Maps.go
Name: John
Person: map[age:30 city:New York name:John surname:Doe]