Sergio Alexander Florez Galeano
System engineer and passionate entrepreneur, with talent and experience building digital business. Speaker, researcher, athlete, gamer, and geek 100%.
https://slides.com/xergioalex
http://rocka.co
xergioalex
Open Source
+
go build
Compiled (Cross-Compile)
&& Multiplatform
C Sintax
C Sintax (Interpreted language styles)
package main
import "fmt"
func main () {
fmt.Printf("Hello World")
}
C Sintax
C Sintax (Interpreted language styles)
package main
import "fmt"
func main () {
fmt.Printf("Hello World")
}
Easy To Learn
Modern language
Designed thinking on modern systems that needs:
- High performance
- High concurrency
The Standard Library
- net/http
- database/sql/driver
- encoding
- testing
- encryption
Gopher Pet :3
Go started in 2007 by
- Robert Griesemer (JVM, V8 Js)
- Rob Pike (UNIX, UTF-8 )
- Khen Thompson
(B, C, UNIX, UTF-8)
- Go was released by Google as an open source project in 2009
- Version 1.0 was released in 2012.
- Go team didn't seek to replace Java, C ++ or any other language, but instead devoted themselves to developing a system that would give them many advantages they were looking for.
- Canonical
- CloudFlre
- Uber
- Disqus
- Digital Ocean
- Facebook
- Netflix
CODING TIME
$ sudo add-apt-repository ppa:longsleep/golang-backports
$ sudo apt-get update
$ sudo apt-get install golang-go
$ go version
go version go1.12 linux/amd64
DEMO
package main
import "fmt"
func main () {
fmt.Printf("Hello World")
}
DEMO
package main
import "fmt"
func main () {
var name string
fmt.Print("Please type your name: ")
fmt.Scanf("%s", &name)
fmt.Printf("Welcome %s to Go Lang", name)
}
DEMO
var name string
var name string = "Name test"
lastname := "Lastname test"
var number = 100
var number1 int
var (
a = 1
b = 2
c = 3
)
DEMO
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
DEMO
func test() bool {
return true
}
func test1() int32 {
return 5
}
func test1(myVar1 int32, myVar2 float32, myVar3 string) int32 {
// ...
return 1000
}
FUNC
DEMO
a, b, c := getVariables()
func getVariables() (int a, int, int) {
return 1, 2, 3
}
// ...
val1, val2, val3, val4 = calc(num1, num2)
func calc(a int32, b int32) (int32, int32, int32, float32) {
return a + b, a - b, a * b, float32(a) / float32(b)
}
DEMO
# Type 1
for i := 0; i < 5; i++ {
fmt.Println("FOR: ", i)
}
# Type 2
c:=100
for ; c > 0; {
c -= 10
}
DEMO
# Type 3
c:=100
for c > 0 {
c -= 10
}
# Type 4
s := 1000
for {
s -= 1
if s == 0 {
break
}
}
DEMO
var number = 0
fmt.Print("Typ a number: ")
fmt.Scanf("%d", &number)
if number %2 == 0 {
fmt.Println("Even number")
} else {
fmt.Println("Odd number")
}
DEMO
switch number {
case 1:
fmt.Println("The number is 1")
default:
fmt.Println("The number isnt 1")
}
//...
switch {
case number%2 == 0:
fmt.Println("The number is even")
default:
fmt.Println("The number is odd")
}
DEMO
switch {
case number == 2:
fmt.Println("The number is 2")
fallthrough
case number%2 == 0:
fmt.Println("The number is even")
default:
fmt.Println("The number is odd")
}
fallthrough
DEMO
var arr1 [2]string
arr1[0] = "value1"
arr1[1] = "value2"
// arr1[2] = "value2" // This will
//throw exception
fmt.Println(arr1)
DEMO
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("My array:", b)
DEMO
var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
DEMO
mapTest := make(map[string] int)
mapTest["key1"] = 1
mapTest["key2"] = 100
fmt.Println(mapTest)
DEMO
mapTest := map[string] int {
"Juan": 18,
"Sergio": 24,
"Julian": 30,
}
delete(mapTest, "Juan")
fmt.Println(mapTest)
DEMO
type Course struct {
Name string
Slug string
Skills [] string
}
myCourse := Course{
Name: "Go",
Slug: "go",
Skills: []string{"1", "2"}
}
DEMO
type Course struct {
Name string
Slug string
Skills [] string
}
myCourse := new(Course)
myCourse.Name = "Go2"
myCourse.Slug = "go2"
myCourse.Skills = []string{"3", "4"}
DEMO
type Course struct {
Name string
Slug string
Skills [] string
}
func (p Course) Subscribe (name string) {
fmt.Printf("The person %s has subscribed to %s course\n", name, p.Name)
}
myCourse := Course{ Name: "Go", Slug: "go", Skills: []string{"1", "2"} }
myCourse.Subscribe("XergioAleX")
fmt.Println(myCourse)
DEMO
type Shape interface {
Area() float64
Perimeter() float64
}
type Rect struct {
width float64
height float64
}
func (r Rect) Area() float64 {
return r.width + r.height
}
func (r Rect) Perimeter() float64 {
return 2 * (r.width + r.height)
}
var s Shape
s = Rect{5.0, 4.0}
fmt.Print("Area", s.Area())
fmt.Print("Perimeter", s.Perimeter())
DEMO
func deferTest () {
fmt.Println("The main function finish")
}
func main() {
defer deferTest() // defer allow do something after the current function context finish
fmt.Println("Hello world")
}
DEMO
func orderDefer() { // Defers works as stack
fmt.Println("Start")
defer defered("1")
defer defered("2")
defer defered("3")
defer defered("4")
defer defered("5")
fmt.Println("End")
}
orderDefer()
DEMO
func main() {
number, err := Div(50, 0)
if err != nil {
panic(err)
}
fmt.Println(number)
}
func Div(a float32, b float32) (float32, error) {
if b == 0 {
return 0, errors.New("Division by 0")
}
return a / 2
}
DEMO
func main() {
a := 100
var b * int
b = &a
fmt.Println(a, *b)
fmt.Println(&a, b)
*c = 10
fmt.Println(a, *b)
fmt.Println(&a, b)
}
DEMO
func helloGo(index int) {
fmt.Printf("Hello, the index is #%d\n", index)
}
func forGo(n int, text string) {
for i := 0; i < n; i++ {
helloGo(i)
}
}
func main () {
forGo(500)
}
...
DEMO
func helloGo(index int) {
fmt.Printf("Hello, i am a goroutine #%d\n", index)
}
func forGo(n int, text string) {
for i := 0; i < n; i++ {
helloGo(i)
}
}
func main () {
forGo(500)
}
go
time.Sleep(1000 * time.Millisecond);
DEMO
ch := make(chan string)
go func () {
defer close(ch)
ch <- "Hi Channel"
}()
fmt.Println(<- ch)
DEMO
package main
import (
"net/http"
"io"
)
func handler (w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hola Servidor Go!")
}
func main () {
http.HandleFunc("/", handler)
http.ListenAndServe(":8000", nil)
}
DEMO
import "net/http"
type Post struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
func main () {
var client = &http.Client{ Timeout: 10 * time.Second }
url := "http://jsonplaceholder.typicode.com/posts"
resp, err := client.Get(url)
//...
var post [] Post
json.NewDecoder(resp.Body).Decode(&post)
fmt.Println(len(post))
fmt.Println(post[0])
}
DEMO
package main
import (
"fmt"
"io/ioutil"
"path"
"runtime"
)
func main() {
// Get information about program execution
_, filename, _, ok := runtime.Caller(0)
// Validate execution
if !ok {
panic("No caller information")
}
/* Read a file */
data, err := ioutil.ReadFile(path.Dir(filename) + "/test.txt")
// Stop execution if error any error happen
if err != nil {
panic(err)
}
// Print file content
fmt.Println(string(data))
}
I Know Go
Most used
Most Used
Simple 2D game library
http://rocka.co
xergioalex
By Sergio Alexander Florez Galeano
Introduction to GoLang
System engineer and passionate entrepreneur, with talent and experience building digital business. Speaker, researcher, athlete, gamer, and geek 100%.