1+ package main
2+
3+ import (
4+ "fmt"
5+ "strconv"
6+ )
7+
8+ var global = "Soy una variable global"
9+
10+ func main () {
11+
12+ Sayhi ()
13+
14+ fmt .Println (Outputname ("Gera" ))
15+
16+ fmt .Println (Twoparams (1 , 2 ))
17+
18+ fmt .Println (Doubleamounts (2 ))
19+
20+ fmt .Println (Comporobartipo (2 ))
21+
22+ Scope ()
23+
24+ extra := Extra ("Fizz" , "Buzz" )
25+
26+ fmt .Println ("El numero de veces que se imprimieron numeros fue: " , extra )
27+
28+ }
29+
30+ // Funcion basica, sin parametros ni retorno
31+ func Sayhi () {
32+ fmt .Println ("Hola mundo" )
33+ }
34+
35+ // Funcion con un parametro con retorno
36+ func Outputname (name string ) string {
37+ //return a string with the name injected
38+ return "Hola usuario " + name
39+ }
40+
41+ // Funcion con parametros y retorno
42+ func Twoparams (param1 int , param2 int ) int {
43+ return param1 + param2
44+ }
45+
46+ // Funcion adentro de otra funcion
47+ func Doubleamounts (amount1 int ) int {
48+ //funcion anonima
49+ Multiplyby := func (number int ) int {
50+ return number * 2
51+ }
52+ return Multiplyby (amount1 )
53+ }
54+
55+ // Funcion ya creada en el lenguaje
56+ func Comporobartipo (param int ) string {
57+ return `El valor de param es ` + strconv .Itoa (param ) + ` y el tipo es ` + fmt .Sprintf ("%T" , param )
58+ }
59+
60+ // Funcion para probar el scope de las variables
61+ func Scope () {
62+ //variable local
63+ var local = "Soy una variable local"
64+ fmt .Println (local )
65+ fmt .Println (global )
66+ }
67+
68+ // Dificultad extra
69+ func Extra (str1 string , str2 string ) int {
70+ count := 0
71+ for i := 1 ; i <= 100 ; i ++ {
72+ if i % 3 == 0 && i % 5 == 0 {
73+ fmt .Println (str1 + str2 )
74+ } else if i % 3 == 0 {
75+ fmt .Println (str1 )
76+ } else if i % 5 == 0 {
77+ fmt .Println (str2 )
78+ } else {
79+ fmt .Println (i )
80+ count ++
81+ }
82+ }
83+ return count
84+ }
0 commit comments