Tod el codigo de loe que vamos del curso por aqui...
package main
import ( "fmt")
func main() { //Declaracion de constantes const pi float64 = 3.14 const pi2 = 3.14 fmt.Println("pi: ", pi2) fmt.Println("pi2: ", pi2)
//Declaracion de variables enteras base := 12 var altura int = 14 var area int
fmt.Println(base, altura, area)
//Zero values
var a int var b float64 var c string var d bool
fmt.Println(a, b, c, d)
//Area cuadrado const baseCuadrado = 10 areaCuadrado := baseCuadrado * baseCuadrado fmt.Println("Area del cuadrado: ", areaCuadrado)
////////////////////////////////////////////////////// //Operadores aritmeticos x := 10 y := 50
//Suma resultado := x + y fmt.Println("Suma: ", resultado)
//Resta resultado = y - x fmt.Println("Resta: ", resultado)
//Multiplicacion resultado = x * y fmt.Println("Suma: ", resultado)
//Divicion resultado = y / x fmt.Println("División: ", resultado)
//Modulo resultado = y % x fmt.Println("Modulo: ", resultado)
//Incremento x++ fmt.Println("Incremento: ", x)
//Divicion x-- fmt.Println("Decremento: ", x)
//Printf nombre := "Platzi" cursos := 500 //%s String //%d entero //%f float64 //%t booleano //%p direccion de memoria //%e numero en notacion cientifica //%x numero en hexadecimal //%o numero en octal //%q cadena entre comillas simples //%c caracter Unicode //%T tipo de dato //%v valor //%p dirección de memoria //%+v agregar nombre al campo //%#v formato en hexadecimal //%[1]v campo con indice 1 //%[2]v campo con indice 2 //... //%n salto de linea //%t indicador de verdadero/falso //%b binario //%x hexadecimal //%o //Se agrega %v cuando no se sabe el tipo de dato fmt.Printf("Hola %s, tienes %d cursos\n", nombre, cursos)
//Sprintf message := fmt.Sprintf("Hola %s, tienes %d cursos\n", nombre, cursos) fmt.Println(message)
//Tipo de dato fmt.Printf("HelloMessage: %T\n", "helloMessage") fmt.Printf("Cursos: %T\n", cursos)
normalFuncion("Hola Mundo") tripleArgumento(1, 2, "tres") result := returnValue(4) fmt.Println("Return value: ", result)
value1, value2 := dobleReturn(2) fmt.Println("Value1: ", value1, "Value2: ", value2)
value3, _ := dobleReturn(2) fmt.Println("Value3: ", value3)
fmt.Println("///// CICLO FOR ") cicloFor()
fmt.Println("///// CICLO FOR WHILE ") cicloForWhile()
fmt.Println("///// CICLO FOR COUNTER FOREVER ") cicloForForever()
fmt.Println("///// OPERADORES LOGICOS ")
var valor1 bool = false var valor2 bool = true var entero1 int = 5 var entero2 int = -5
eq := igualdad(valor1, valor2) ds := distinto(valor1, valor2)
comp1, comp2, comp3, comp4 := comparacion(entero1, entero2) andGate, orGate, notGate := logicGates(valor1, valor2)
fmt.Printf("%t == %t : %t\n", valor1, valor2, eq) fmt.Printf("%t != %t : %t\n", valor1, valor2, ds) fmt.Printf("%d > %d : %t\n", entero1, entero2, comp1) fmt.Printf("%d < %d : %t\n", entero1, entero2, comp2) fmt.Printf("%d >= %d : %t\n", entero1, entero2, comp3) fmt.Printf("%d <= %d : %t\n", entero1, entero2, comp4) fmt.Printf("%t && %t : %t\n", valor1, valor2, andGate) fmt.Printf("%t || %t : %t\n", valor1, valor2, orGate) fmt.Printf("!%t : %t\n", valor1, notGate)
}
// ////////////////////////////////////////////////////// Funcinesfunc normalFuncion(message string) { fmt.Println(message)}
func tripleArgumento(a int, b int, c string) { fmt.Println(a, b, c)}
func returnValue(a int) int { return a * 3}
func dobleReturn(a int) (c, d int) { return a, a * 2}func dobleReturn2(a int) (c, d int) { return a, a * 2}
// ////////////////////////////////////////////////////// Ciclos
func cicloFor() { for i := 0; i < 10; i++ { fmt.Println(i) }}
func cicloForWhile() { counter := 0 for counter < 10 { fmt.Println(counter) counter++ }}
func cicloForForever() { counterForever := 0 for { fmt.Println(counterForever) counterForever++ /* if counterForever == 1000 { break } */
if counterForever == 100 { break } }}
// ////////////////////////////////////////////////////// Operadores Logicos
func igualdad(v1, v2 bool) bool { return v1 == v2}
func distinto(v1, v2 bool) bool { return v1 != v2}
func comparacion(i1, i2 int) (ans1, ans2, ans3, ans4 bool) { return i1 > i2, i1 < i2, i1 >= i2, i1 <= i2}
func logicGates(v1, v2 bool) (ans1, ans2, ans3 bool) { return v1 && v2, v1 || v2, !v1}