├── .DS_Store ├── 01_hola └── hola.go ├── 02_variables └── main.go ├── 03_constantes └── main.go ├── 04_condicionales ├── 01_if │ └── main.go └── 02_switch │ └── main.go ├── 05_arrays └── main.go ├── 06_slices └── main.go ├── 07_maps └── main.go ├── 08_estructuras └── main.go ├── 09_for ├── 01_for_clasico │ └── main.go ├── 02_for_continuo │ └── main.go ├── 03_for_forever │ └── main.go └── 04_for_range │ └── main.go ├── 10_funciones ├── 01_declaracion │ └── main.go ├── 02_retornos │ └── main.go ├── 03_variaticas │ └── main.go └── 04_anonimas │ └── main.go ├── 11_errores └── main.go ├── 12_punteros └── main.go ├── 13_paquetes ├── despedida │ └── despedida.go ├── main.go └── saludar │ └── saludar.go ├── 14_metodos └── main.go ├── 15_interfaces ├── animales │ ├── gato.go │ ├── interfaces.go │ └── perro.go └── main.go ├── 16_defer └── main.go ├── 17_panic └── main.go ├── 18_recover └── main.go ├── 19_concurrencia └── main.go ├── 19_web ├── 01_servidor │ ├── main.go │ └── public │ │ └── index.html ├── 02_handlers │ └── main.go ├── 03_handlerfunc │ └── main.go ├── 04_htmltemplate │ ├── main.go │ └── views │ │ └── index.html └── 05_gorm │ └── main.go ├── 20_paralelismo └── main.go ├── 21_canales └── main.go └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dickson7/go/51876b99ce34da7ff0c80f6951b742dcbefeccd3/.DS_Store -------------------------------------------------------------------------------- /01_hola/hola.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hola, Primer Hola Mundo en GoLang ") 7 | } 8 | -------------------------------------------------------------------------------- /02_variables/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //primer manera de definir variables 7 | var nombre, apellido string 8 | nombre= "Dickson" 9 | apellido = "Garcia" 10 | //segunda manera de definir variables 11 | apellido2 := "Rincon" 12 | //tercera manera de definir variables 13 | usuario1 , usuario2 := "emely", "joaquin" 14 | 15 | fmt.Println(nombre, apellido, apellido2) 16 | fmt.Println(usuario1, usuario2) 17 | } 18 | -------------------------------------------------------------------------------- /03_constantes/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | /* 7 | El valor de una constante no 8 | se pueden cambiar 9 | */ 10 | //Comentarios de linea para documentar codigo 11 | 12 | const nombre = "Dickson" 13 | 14 | fmt.Println(nombre) 15 | 16 | //valor cero de variables 17 | var nomb string 18 | var numero int 19 | var entendes bool 20 | fmt.Println(nomb, numero, entendes) 21 | } 22 | -------------------------------------------------------------------------------- /04_condicionales/01_if/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // or || 6 | // and && 7 | // not ! 8 | 9 | func main() { 10 | if 5 < 10 { 11 | fmt.Println("Esto esta en el bloque verdadero") 12 | } 13 | //valida la antrada a bloque de codigo 14 | if isValid := true; isValid { 15 | fmt.Println("Esto esta entrando en el bloque verdadero") 16 | } else { 17 | fmt.Println("Bloque falso") 18 | } 19 | fmt.Println("Fin del programa") 20 | } 21 | -------------------------------------------------------------------------------- /04_condicionales/02_switch/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | //ejemplo 1 8 | /* 9 | var a uint8 10 | a = 3 11 | switch a { 12 | case 1: 13 | fmt.Println("Domingo") 14 | case 2: 15 | fmt.Println("Lunes") 16 | case 3: 17 | fmt.Println("Martes") 18 | case 4: 19 | fmt.Println("Miercoles") 20 | case 5: 21 | fmt.Println("Jueves") 22 | case 6: 23 | fmt.Println("Viernes") 24 | case 7: 25 | fmt.Println("Sabado") 26 | default: 27 | fmt.Println("No es un dia de la semana") 28 | } 29 | */ 30 | 31 | //Ejemplo 2 32 | /* 33 | var a uint8 34 | a = 3 35 | switch a { 36 | case 1: 37 | fallthrough 38 | case 2: 39 | fallthrough 40 | case 3: 41 | fallthrough 42 | case 4: 43 | fallthrough 44 | case 5: 45 | fmt.Println("Estas entre semana") 46 | case 6: 47 | fallthrough 48 | case 7: 49 | fmt.Println("Es fin de semana") 50 | default: 51 | fmt.Println("No es un dia valido") 52 | } 53 | */ 54 | 55 | //Ejemplo 3 56 | switch a := 7; { 57 | case a >= 0 && a <= 5: 58 | fmt.Println("Estas entres semana") 59 | case a >= 6 && a <= 7: 60 | fmt.Println("Estas en fin de semana") 61 | default: 62 | fmt.Println("No es un dia valido") 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /05_arrays/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //En los arrays todos los valores son de 7 | //tamaño fijo y del mismo tipo de datos 8 | var nombres [3]string 9 | nombres[0] = "Dickson" 10 | nombres[1] = "Emely" 11 | nombres[2] = "Joaquin" 12 | 13 | fmt.Println(nombres) 14 | 15 | apellidos := [3]string{"Garcia", "Zerpa", "GarciaZerpa"} 16 | 17 | fmt.Println(apellidos) 18 | fmt.Println(nombres[1], apellidos[1]) 19 | 20 | //tamano del arreglo para conocer su tamaño 21 | size := len(nombres) 22 | fmt.Println("El tamaño de arreglo es de: ", size) 23 | 24 | //fmt.Println(nombres[inicio:fina(excluyente)]) 25 | fmt.Println(nombres[0:2]) 26 | } 27 | -------------------------------------------------------------------------------- /06_slices/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //slices 7 | //tiene un apuntador a un arrays 8 | // tienen un tamaño 9 | //tambien tiene una capacidad 10 | 11 | //Declaracion de slices 12 | // var nombres []string 13 | 14 | //otra manera de declarar slices 15 | nombres := make([]string, 0) 16 | fmt.Printf("Su tamaño es: %d y scapacidad es: %d\n", len(nombres), cap(nombres)) 17 | nombres = append(nombres, "Dickson") 18 | fmt.Printf("Su tamaño es: %d y scapacidad es: %d\n", len(nombres), cap(nombres)) 19 | nombres = append(nombres, "Arley") 20 | fmt.Printf("Su tamaño es: %d y scapacidad es: %d\n", len(nombres), cap(nombres)) 21 | nombres = append(nombres, "Emely") 22 | fmt.Printf("Su tamaño es: %d y scapacidad es: %d\n", len(nombres), cap(nombres)) 23 | nombres = append(nombres, "Yoxandra") 24 | fmt.Printf("Su tamaño es: %d 1y scapacidad es: %d\n", len(nombres), cap(nombres)) 25 | nombres = append(nombres, "Joaquin") 26 | fmt.Printf("Su tamaño es: %d 1y scapacidad es: %d\n", len(nombres), cap(nombres)) 27 | nombres = append(nombres, "Gerald") 28 | 29 | //Otra forma de agregar informacion a nuestro slices 30 | apellidos := []string{ 31 | "Garcia", 32 | "Zerpa", 33 | "Rincon", 34 | } 35 | 36 | fmt.Println(nombres) 37 | fmt.Println(apellidos) 38 | } 39 | -------------------------------------------------------------------------------- /07_maps/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //Declaracion de maps 7 | /* 8 | idiomas := make(map[string]string) 9 | idiomas["es"] = "Español" 10 | idiomas["en"] = "Inglés" 11 | idiomas["fr"] = "Frecés" 12 | */ 13 | 14 | //otra forma de Declaracion de maps 15 | idiomas := map[string]string{ 16 | "es": "Español", 17 | "en": "Inglés", 18 | "fr": "Francés", 19 | } 20 | //imprimir todo el mapa 21 | fmt.Println(idiomas) 22 | //imprimir solo una posicion en especifico 23 | fmt.Println(idiomas["fr"]) 24 | //eliminar una posicion del mapa 25 | delete(idiomas, "en") 26 | fmt.Println(idiomas) 27 | 28 | // comprobar si una posicion existe 29 | if idioma, ok := idiomas["en"]; ok { 30 | fmt.Println("La posicion en si existe y vale ", idioma) 31 | } else { 32 | fmt.Println("La posicion NO existe") 33 | } 34 | 35 | //declaracion de maps con int y string 36 | numeros := map[int]string{ 37 | 1: "un numero pequeño", 38 | 20: "otro numero", 39 | -3: "llave negativa", 40 | } 41 | fmt.Println(numeros) 42 | } 43 | -------------------------------------------------------------------------------- /08_estructuras/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // Persona es una estructura 6 | type Persona struct { 7 | Nombre string 8 | Edad uint8 9 | Emails []string 10 | } 11 | 12 | func main() { 13 | // 1. forma de declarar y agregar campos a la estructura 14 | /* 15 | var persona1 Persona 16 | persona1.Nombre = "Dickson" 17 | persona1.Edad = 30 18 | fmt.Println(persona1) 19 | fmt.Println(persona1.Edad) 20 | */ 21 | 22 | // 2. Manera con shorthand 23 | /* 24 | persona2 := Persona{} 25 | persona2.Nombre = "Dickson" 26 | persona2.Edad = 30 27 | fmt.Println(persona2) 28 | */ 29 | 30 | // 3. forma con los datos dentro de las llaves 31 | /* 32 | persona2 := Persona{ 33 | Nombre: "Dickson", 34 | Edad: 31, 35 | } 36 | fmt.Println(persona2) 37 | */ 38 | 39 | // 4. forma con los datos dentro de las llaves 40 | persona2 := Persona{ 41 | "Dickson", 42 | 32, 43 | []string{"a@b.com", "d@b.com"}, 44 | } 45 | fmt.Println(persona2) 46 | } 47 | -------------------------------------------------------------------------------- /09_for/01_for_clasico/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // ejemplo 1 7 | /* 8 | count := 5 9 | for i := 0; i < count; i++ { 10 | fmt.Println(i) 11 | } 12 | */ 13 | 14 | // ejemplo 2 15 | /* 16 | for i := 5; i >= 0; i-- { 17 | if i == 3 { 18 | fmt.Println("Se salta el valor 3") 19 | continue 20 | } 21 | fmt.Println(i) 22 | } 23 | */ 24 | 25 | // ejemplo 3 26 | /* 27 | count := 5 28 | for i := 0; i < count; i++ { 29 | if i == 2 { 30 | fmt.Println("Cuando i vale 2 se rompe el ciclo") 31 | break 32 | } 33 | fmt.Println(i) 34 | } 35 | */ 36 | 37 | // ejemplo 4 38 | matriz := [3][3]int{} 39 | valor := 0 40 | for i := 0; i < 3; i++ { 41 | for y := 0; y < 3; y++ { 42 | valor++ 43 | matriz[i][y] = valor 44 | } 45 | 46 | } 47 | for i := 0; i < 3; i++ { 48 | for y := 0; y < 3; y++ { 49 | fmt.Println(matriz[i][y]) 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /09_for/02_for_continuo/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // For continuo es similar a un while en otros lenguajes 7 | a := 5 8 | for a > 0 { 9 | a-- 10 | fmt.Println(a) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /09_for/03_for_forever/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // for infinito se ejecuta infinitamente y no hay 7 | // ninguna condicion que lo detenga 8 | // Se utiliza para escuchar permanentemente conexiones 9 | // a la base de datos, sockets etc 10 | for { 11 | fmt.Println("Esto no se va a detener") 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /09_for/04_for_range/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | numeros := []string{"cero", "uno", "dos", "tres"} 8 | 9 | // el range devuelve dos valores el indice y el valor 10 | for i, v := range numeros { 11 | fmt.Println(i, v) 12 | } 13 | 14 | //si no queremeos utilizar el indice de coloca _ 15 | for _, v := range numeros { 16 | fmt.Println(v) 17 | } 18 | 19 | // El valor se puede omitir en un range 20 | for i := range numeros { 21 | fmt.Println(i) 22 | } 23 | 24 | //ejemplo con mapas funciona de la misma forma 25 | nombres := map[string]string{ 26 | "es": "España", 27 | "co": "Colombia", 28 | "br": "Brasil", 29 | } 30 | for i, v := range nombres { 31 | fmt.Println(i, v) 32 | } 33 | 34 | // Iteracion de string 35 | frase := "Hola Mundo" 36 | for posicion, letra := range frase { 37 | //fmt.Println(posicion, letra) 38 | fmt.Println(posicion, string(letra)) 39 | } 40 | 41 | // Iterar slices de entreros 42 | for _, entero := range []int{15, 36, 24, 87} { 43 | fmt.Println(entero) 44 | } 45 | 46 | // Iterar un string 47 | for _, letra := range "Hola Mundo" { 48 | fmt.Println(string(letra)) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /10_funciones/01_declaracion/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | saludar("Dickson", 31) 7 | } 8 | 9 | func saludar(nombre string, edad uint8) { 10 | fmt.Println("Hola ", nombre) 11 | fmt.Printf("Tienes %d años\n", edad) 12 | if edad > 30 { 13 | return 14 | } 15 | fmt.Println("Eres joven") 16 | } 17 | -------------------------------------------------------------------------------- /10_funciones/02_retornos/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var n1, n2 int8 7 | n1 = 10 8 | n2 = 17 9 | 10 | r := suma(n1, n2) 11 | fmt.Println(r) 12 | 13 | var edad uint8 14 | edad = 3 15 | fmt.Println(tipoEdad(edad)) 16 | 17 | var n []uint8 18 | n = []uint8{8, 2, 6, 7, 9, 1} 19 | maximo, minimo := maxymin(n) 20 | fmt.Printf("El valor mayor es %d y el valor menor %d \n", maximo, minimo) 21 | 22 | } 23 | 24 | func suma(a, b int8) int8 { 25 | return a + b 26 | } 27 | 28 | func tipoEdad(edad uint8) string { 29 | var tipo string 30 | switch { 31 | case edad < 12: 32 | tipo = "Niñez" 33 | case edad < 18: 34 | tipo = "Adolecencia" 35 | default: 36 | tipo = "Madurez" 37 | } 38 | return tipo 39 | } 40 | 41 | // manera de firmar las funciones 42 | /* 43 | func maxymin(numeros []uint8) (uint8, uint8) { 44 | var maxi, mini uint8 45 | for _, v := range numeros { 46 | if v > maxi { 47 | maxi = v 48 | } 49 | if v < mini { 50 | mini = v 51 | } 52 | } 53 | return maxi, mini 54 | } 55 | */ 56 | // De esta manera es mas legible que retorna 57 | func maxymin(numeros []uint8) (maxi uint8, mini uint8) { 58 | for _, v := range numeros { 59 | if v > maxi { 60 | maxi = v 61 | } 62 | if v < mini { 63 | mini = v 64 | } 65 | } 66 | return 67 | } 68 | -------------------------------------------------------------------------------- /10_funciones/03_variaticas/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | saludarVarios(10, "dickson", "emely", "jaoquin") 7 | } 8 | 9 | //las funcions variaticas solo recibe un unico parametro variatico 10 | func saludarVarios(edad uint8, nombres ...string) { 11 | // para saber que tivo de datos es una variable 12 | fmt.Printf("%T\n", nombres) 13 | for _, v := range nombres { 14 | fmt.Println("Hola", v, "edad", edad) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /10_funciones/04_anonimas/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | /* 7 | anonima := func() { 8 | fmt.Println("Me imprimo en una variable") 9 | } 10 | anonima() 11 | */ 12 | 13 | secuencia1 := secuencia() 14 | fmt.Println(secuencia1()) 15 | fmt.Println(secuencia1()) 16 | fmt.Println(secuencia1()) 17 | fmt.Println("__________") 18 | secuencia2 := secuencia() 19 | fmt.Println(secuencia2()) 20 | fmt.Println(secuencia2()) 21 | fmt.Println(secuencia2()) 22 | fmt.Println(secuencia2()) 23 | fmt.Println(secuencia2()) 24 | fmt.Println(secuencia2()) 25 | } 26 | 27 | func secuencia() func() int32 { 28 | var x int32 29 | return func() int32 { 30 | x++ 31 | return x 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /11_errores/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | func main() { 9 | /* 10 | err := errors.New("Mi mensaje de error") 11 | fmt.Printf("%T\n", err) 12 | */ 13 | r, err := division(500, 0) 14 | if err != nil { 15 | fmt.Println("Error:", err) 16 | return 17 | } 18 | fmt.Println(r) 19 | } 20 | 21 | func division(dividendo, divisor float64) (resultado float64, err error) { 22 | if divisor == 0 { 23 | err = errors.New("No se puede dividir por cero") 24 | } else { 25 | resultado = dividendo / divisor 26 | } 27 | return 28 | } 29 | -------------------------------------------------------------------------------- /12_punteros/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | a := 3 7 | fmt.Println("antes de duplicar, a =", a) 8 | duplicar(&a) 9 | fmt.Println("Despues de duplicar, a =", a) 10 | } 11 | 12 | func duplicar(a *int) { 13 | *a *= 2 14 | fmt.Println("Dentro de la funcion deplicar a =", *a) 15 | } 16 | -------------------------------------------------------------------------------- /13_paquetes/despedida/despedida.go: -------------------------------------------------------------------------------- 1 | package depedida 2 | 3 | import "fmt" 4 | 5 | // Despedida se despido de una persona 6 | func Despedirse(nombre string) { 7 | fmt.Println("Hasta luego", nombre) 8 | } 9 | -------------------------------------------------------------------------------- /13_paquetes/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | depedida "github.com/dickson7/go/13_paquetes/despedida" 7 | "github.com/dickson7/go/13_paquetes/saludar" 8 | ) 9 | 10 | func main() { 11 | 12 | saludar.Saludar("Dickson") 13 | saludar.MeVes = "Esto es un string asignado desde el main a la variable en el paquete" 14 | fmt.Println(saludar.MeVes) 15 | 16 | nombre := "arley" 17 | depedida.Despedirse(nombre) 18 | } 19 | -------------------------------------------------------------------------------- /13_paquetes/saludar/saludar.go: -------------------------------------------------------------------------------- 1 | package saludar 2 | 3 | import "fmt" 4 | 5 | // MeVes variable para utilizar fuera del paquete 6 | var MeVes string 7 | 8 | // Saludar saluda a una persona 9 | func Saludar(nombre string) { 10 | fmt.Println("Hola", nombre) 11 | } 12 | -------------------------------------------------------------------------------- /14_metodos/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Persona struct { 6 | nombre string 7 | edad int8 8 | } 9 | 10 | type Numero int 11 | 12 | // Firma del metodp 13 | func (p Persona) saludar() { 14 | fmt.Println("Hola soy una persona") 15 | } 16 | 17 | func (n Numero) presentarse() { 18 | fmt.Println("Soy un numero") 19 | } 20 | 21 | func (p *Persona) asignarEdad(i int8) { 22 | if i < 0 { 23 | fmt.Println("La edad negativa no es permitida") 24 | } else { 25 | p.edad = i 26 | } 27 | } 28 | 29 | func main() { 30 | var persona Persona 31 | var numero Numero 32 | 33 | persona.saludar() 34 | numero.presentarse() 35 | 36 | persona.asignarEdad(-30) 37 | fmt.Println(persona) 38 | } 39 | -------------------------------------------------------------------------------- /15_interfaces/animales/gato.go: -------------------------------------------------------------------------------- 1 | package animales 2 | 3 | import "fmt" 4 | 5 | type Gato struct { 6 | Nombre string 7 | } 8 | 9 | func (g Gato) Comunicarse() { 10 | fmt.Println("Miauuuu") 11 | } 12 | -------------------------------------------------------------------------------- /15_interfaces/animales/interfaces.go: -------------------------------------------------------------------------------- 1 | package animales 2 | 3 | type Mascota interface { 4 | Comunicarse() 5 | } 6 | -------------------------------------------------------------------------------- /15_interfaces/animales/perro.go: -------------------------------------------------------------------------------- 1 | package animales 2 | 3 | import "fmt" 4 | 5 | type Perro struct { 6 | Nombre string 7 | } 8 | 9 | func (p Perro) Comunicarse() { 10 | fmt.Println("Wooffff") 11 | } 12 | -------------------------------------------------------------------------------- /15_interfaces/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/dickson7/go/15_interfaces/animales" 4 | 5 | func main() { 6 | var p animales.Perro 7 | var g animales.Gato 8 | 9 | p.Nombre = "Terry" 10 | g.Nombre = "firulais" 11 | 12 | // AdoptarPerro(p) 13 | // AdoptarGato(g) 14 | AdoptarMascota(p) 15 | AdoptarMascota(g) 16 | } 17 | 18 | func AdoptarMascota(m animales.Mascota) { 19 | m.Comunicarse() 20 | } 21 | 22 | /* 23 | func AdoptarPerro(p animales.Perro) { 24 | p.Comunicarse() 25 | } 26 | 27 | func AdoptarGato(g animales.Gato) { 28 | g.Comunicarse() 29 | } 30 | */ 31 | -------------------------------------------------------------------------------- /16_defer/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // Defer es como una sala de espera 7 | // que hace la ejecucion de codigo cuando se termina la funcion 8 | 9 | fmt.Println("Conectando a la base de datos...") 10 | defer cerrarDB() 11 | 12 | fmt.Println("Consultamos informacion de set de datos") 13 | defer cerrarSetDeDatos() 14 | } 15 | 16 | func cerrarDB() { 17 | fmt.Println("Cerrar la BD") 18 | } 19 | 20 | func cerrarSetDeDatos() { 21 | fmt.Println("Cerrar el set de datos") 22 | } 23 | -------------------------------------------------------------------------------- /17_panic/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | division(3, 0) 7 | } 8 | 9 | func division(dividendo, divisor int) { 10 | defer fmt.Println("Esto se ejecutara pase lo que pase") 11 | if divisor == 0 { 12 | panic("No se puede dividir por cero") 13 | } 14 | fmt.Println(dividendo / divisor) 15 | 16 | } 17 | -------------------------------------------------------------------------------- /18_recover/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | f() 7 | } 8 | 9 | func f() { 10 | defer func() { 11 | if r := recover(); r != nil { 12 | fmt.Printf("%T\n", r) 13 | fmt.Println("Recuperado en f:", r) 14 | fmt.Println("Se llama a la funcion de respaldo para continuar la app") 15 | } 16 | }() 17 | 18 | fmt.Println("Llamando a g.") 19 | g(5) 20 | fmt.Println("Retorna normalmente desde g") 21 | } 22 | 23 | func g(i int) { 24 | if i > 3 { 25 | fmt.Println("paaaaaaaaanicooooo") 26 | panic("el numero no puede ser mayor a 3") 27 | } 28 | defer fmt.Println("Defer en la funcion g") 29 | fmt.Println("Imprimiendo en g:", i) 30 | } 31 | -------------------------------------------------------------------------------- /19_concurrencia/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | var h string 10 | go MostrarNumeros() 11 | fmt.Println("Digita algo:") 12 | fmt.Scan(&h) 13 | fmt.Println("Digitaste:", h) 14 | } 15 | 16 | func MostrarNumeros() { 17 | for i := 1; i <= 10; i++ { 18 | fmt.Println(i) 19 | time.Sleep(time.Second) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /19_web/01_servidor/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | ) 7 | 8 | func main() { 9 | 10 | // servidor web de forma tradicional 11 | /* 12 | // Ruta a nuestro servidor que va a ser la raiz "/" y que debe hacer al realizar esa petición 13 | http.Handle("/", http.FileServer(http.Dir("public"))) 14 | log.Println("Ejucutado server en http://localhost:8080") 15 | // se carga el servidor diciendole que se sirva en un puerto 16 | // si el puerto esta ocupado va adevolver un error y para capturar ese error lo hacemos con un log 17 | log.Println(http.ListenAndServe(":8080", nil)) 18 | */ 19 | 20 | // servidor web mejorado con un multiplexor(ayuda a resolver las solucitudes) 21 | // server mux resuelve mas peticiones 22 | mux := http.NewServeMux() 23 | fs := http.FileServer(http.Dir("public")) 24 | mux.Handle("/", fs) 25 | log.Println("Ejucutado server en http://localhost:8080") 26 | log.Println(http.ListenAndServe(":8080", mux)) 27 | } 28 | -------------------------------------------------------------------------------- /19_web/01_servidor/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |