Go is the programming language developed by Google and released in 2009. It is available for various Unix-like systems, including FreeBSD, Linux, Mac OS X, and Plan 9, as well as different architectures such as i386, amd64, and ARM.
The main advantages compared to other programming languages are:
- Higher performance than Python, Ruby, or JavaScript.
- Better support for concurrent work.
- Go runtime is included in the binaries, making it highly distributable across systems.
- Decentralized libraries; each programmer hosts their code wherever they prefer. To install the software, access to the respective platform is done directly.
-
Installation
-
Hello world
-
Variables and data types
-
Flow control
-
Functions
-
Arrays and slices
-
Maps
-
Additional resources
Installation:
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 and data types:
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
Flow control:
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
Functions:
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 and 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]