From 674de3f16599e43e7109dce18b0395429679cbec Mon Sep 17 00:00:00 2001 From: Amador Quispe Date: Sat, 15 Jun 2024 06:32:56 -0500 Subject: [PATCH 1/2] #03 - GO --- .../go/AmadorQuispe.go | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 Roadmap/02 - FUNCIONES Y ALCANCE/go/AmadorQuispe.go diff --git a/Roadmap/02 - FUNCIONES Y ALCANCE/go/AmadorQuispe.go b/Roadmap/02 - FUNCIONES Y ALCANCE/go/AmadorQuispe.go new file mode 100644 index 0000000000..b8b365a3a8 --- /dev/null +++ b/Roadmap/02 - FUNCIONES Y ALCANCE/go/AmadorQuispe.go @@ -0,0 +1,206 @@ +package main + +import ( + "fmt" + "math" + "strconv" + "strings" +) + +var user = "Amador" //Esto es una variable global + +/*EXTRA*/ +func fizzBuzz(s1, s2 string) int { + var count int + for i := 1; i <= 100; i++ { + if i%3 == 0 && i%5 == 0 { + fmt.Println(i, s1, s2) + } else if i%3 == 0 { + fmt.Println(i, s1) + } else if i%5 == 0 { + fmt.Println(i, s2) + } else { + fmt.Println(i) + count++ + } + } + return count +} + +func main() { + fmt.Println("(variable global) Hola !馃憢", user) + fmt.Println("Se ha impreso n煤mero: ", fizzBuzz("fizz", "buzz"), "veces") + welcomeSystem() + printMessage("El sistema puede hacer estos trabajos") + printMessage("[A] Operaci贸n simple (+,-,* y / de dos n煤meros) ") + printMessage("[B] Operaci贸n multiple (+, - y * de varios n煤meros) ") + printMessage("[C] Factorial de un n煤mero") + fmt.Print("Ingresa la opci贸n :") + var option string + fmt.Scanf("%s", &option) + printMessage("La opci贸n que seleccionaste es " + option) + + if option == "A" { + printMessage("Dentro de las operaciones simples tenemos :") + printMessage("(1) Suma") + printMessage("(2) Resta") + printMessage("(3) Multiplicaci贸n") + printMessage("(4) Divisi贸n") + printMessage("(5) Potencia") + fmt.Print("Ingrese el n煤mero de la operaci贸n :") + var opSelect string + var num1, num2 float64 + fmt.Scan(&opSelect) + printMessage("Haz seleccionado " + operationSelected(opSelect) + ", ahora ingresa los n煤meros.") + fmt.Print("Ingrese el n煤mero 1 :") + fmt.Scan(&num1) + fmt.Print("Ingrese el n煤mero 2 :") + fmt.Scan(&num2) + result := operation(opSelect, num1, num2) + printMessage("El resultado de la operaci贸n " + operationSelected(opSelect) + " :") + fmt.Printf("%v y %v es %v \n", num1, num2, result) + } else if option == "B" { + printMessage("Dentro de las operaciones multiples tenemos :") + printMessage("(1) Suma") + printMessage("(2) Resta") + printMessage("(3) Multiplicaci贸n") + fmt.Print("Ingrese el n煤mero de la operaci贸n :") + var opSelect string + var input string + fmt.Scan(&opSelect) + printMessage("Haz seleccionado " + operationSelected(opSelect) + ", ahora ingresa los n煤meros.") + fmt.Print("Ingresa los n煤meros separado por una coma (,) ejemplo 1,2,3 : ") + fmt.Scan(&input) + numbers := stringToSlice(input) + result := operationsMultipleNumbers(opSelect, numbers...) + printMessage("El resultado de la operaci贸n " + operationSelected(opSelect) + " :") + fmt.Println(""+input+" es: ", result) + } else if option == "C" { + printMessage("Factorial de un n煤mero :") + fmt.Print("Ingresa un n煤mero positivo :") + var number int = 5 + fmt.Scan(&number) + result := factorial(number) + fmt.Println("El factorial de ", number, " es ", result) + } else { + printMessage("La opci贸n que seleccionaste no existe") + } + + var msg = exitSystem() + fmt.Println(msg) + + //En go se puede asignar una funci贸n a una variable + var fn = greeting + fn("Amador") + + //Funci贸n an贸nima + fn1 := func() { + fmt.Println("Hello") + } + fn1() + +} + +/*Funci贸n sin par谩metro ni retorno.*/ +func welcomeSystem() { + fmt.Println("------------------------------") + fmt.Println("----Bienvenido al sistema-----") + fmt.Println("------------------------------") +} + +/*Funci贸n que recibe un par谩metro pero no retorna ning煤n valor.*/ +func printMessage(message string) { + fmt.Println(message) +} + +/*Funci贸n sin par谩metro y con retorno.*/ +func exitSystem() string { + return "Gracias por usar el sistema regresa pronto." +} + +/* Funci贸n que recibe un par谩metro y retorna un valor.*/ +func operationSelected(input string) string { + var operation string + switch input { + case "1": + operation = "(1) Suma" + case "2": + operation = "(2) Resta" + case "3": + operation = "(3) Multiplicaci贸n" + case "4": + operation = "(4) Divisi贸n" + case "5": + operation = "(5) Potencia" + + default: + operation = "No soportado" + } + return operation +} + +/*Funci贸n que recibe varios par谩metros y retorna un valor.*/ +func operation(operator string, number1, number2 float64) float64 { + switch operator { + case "1": + return number1 + number2 + case "2": + return number1 - number2 + case "3": + return number1 * number2 + case "4": + return number1 / number2 + case "5": + // Aqu铆 usamos una funci贸n propio del lenguaje + return math.Pow(number1, number2) + + default: + return 0 + } +} + +/*Funci贸n que recibe varios par谩metros (incluido rest parameter) y retorna un valor.*/ +func operationsMultipleNumbers(operator string, numbers ...float64) float64 { + var accumulator float64 + switch operator { + case "1": + for _, v := range numbers { + accumulator += v + } + case "2": + for _, v := range numbers { + accumulator -= v + } + case "3": + for _, v := range numbers { + accumulator *= v + } + default: + accumulator = 0 + + } + return accumulator +} + +func factorial(num int) int { + if num <= 1 { + return 1 + } + return num * factorial(num-1) +} + +func stringToSlice(s string) []float64 { + var numbers []float64 + stringSplit := strings.Split(s, ",") + + for _, v := range stringSplit { + if fv, err := strconv.ParseFloat(v, 64); err == nil { + numbers = append(numbers, fv) + } + } + return numbers +} + +func greeting(name string) { + fmt.Println(name) +} From f11f185b3819ea5de35768bb2af294ea66a04250 Mon Sep 17 00:00:00 2001 From: Amador Quispe Date: Sun, 16 Jun 2024 09:01:30 -0500 Subject: [PATCH 2/2] #24 - GO --- Roadmap/24 - DECORADORES/go/AmadorQuispe.go | 67 +++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Roadmap/24 - DECORADORES/go/AmadorQuispe.go diff --git a/Roadmap/24 - DECORADORES/go/AmadorQuispe.go b/Roadmap/24 - DECORADORES/go/AmadorQuispe.go new file mode 100644 index 0000000000..ccdf72758e --- /dev/null +++ b/Roadmap/24 - DECORADORES/go/AmadorQuispe.go @@ -0,0 +1,67 @@ +package main + +import "fmt" + +type ICoffee interface { + getPrice() uint +} + +type CoffeeSimple struct{} + +func (c *CoffeeSimple) getPrice() uint { + return 25 +} + +type CoffeeWithMilk struct { + coffee ICoffee +} + +func (c *CoffeeWithMilk) getPrice() uint { + additional := 5 + return c.coffee.getPrice() + uint(additional) +} + +type CoffeeWithSugar struct { + coffee ICoffee +} + +func (c *CoffeeWithSugar) getPrice() uint { + additional := 2 + return c.coffee.getPrice() + uint(additional) +} + +//EXTRA +type CoffeeWithCounting struct { + coffee ICoffee + count uint +} + +func (c *CoffeeWithCounting) getPrice() uint { + c.count++ + return c.coffee.getPrice() +} + +func main() { + //Coffee Simple + coffee := &CoffeeSimple{} + fmt.Println("Price coffee normal", coffee.getPrice()) + //Add Milk + coffeeWithMilk := CoffeeWithMilk{ + coffee: coffee, + } + fmt.Println("Price coffee with milk", coffeeWithMilk.getPrice()) + //Add sugar + coffeeWithSugar := CoffeeWithSugar{ + coffee: &coffeeWithMilk, + } + fmt.Println("Price coffee with milk and sugar", coffeeWithSugar.getPrice()) + //Add counting calls + coffeeWithCounting := CoffeeWithCounting{ + coffee: &coffeeWithSugar, + } + coffeeWithCounting.getPrice() + coffeeWithCounting.getPrice() + coffeeWithCounting.getPrice() + fmt.Println("getPrice method has been called ", coffeeWithCounting.count) + +}